My New Year Resolutions in November.

It is just November. And I want to make my new year resolutions today. And I do not want to wait for new years to do things I want to do. And I want to stick to them. So here goes:

1. Understand computer networks.
2. Stick to one side-project.
3. Do more open-source contributions.
4. Read at least 4 books and at max 6 books(has to be non-technincal) until my next birthday (i.e Nov 15, 2015).

I will spend the next few days to research about the best books to read for understanding computer networks. I will also find out the first book(1st of the 4) which I want to start with. And will also decide on a side project. I already have an idea, I just have to find out if it will really solve a problem. Lets just say it is in stealth mode.
And for open-source contributions, I already know few projects I want to contribute to.
If you have any suggestions for books to read, please mention them in comments. I will be grateful.

This all sounds too much to pull-off, but if I could, it will turn out to be an awesome year.

Contributing to Open-Source Projects

Today I would like to share what I have learned from the very little open-source contributions I have made so far. But before I do that, I would like to write a little bit about myself. I am ruby on rails developer at JoshSoftware for the past 3 years. Before that I was studying Electronics in Pune Institute of Computer Technology, Pune. Yes.. you read that right. I was an electronics student in computers college. The reason I am laying out the details is, I do not want you to think that I am some big shot who has been working for a decade. I am just a guy who likes to write programs and do web development.

So, here is the list of hurdles I came across until I made my first open-source contribution:

1. Which projects should I contribute to?
Thats a valid question. There are tons of projects out there. How do I find out about these projects? In my case I have been very fortunate to work on open-source technologies, and at a company that promotes doing contributions. My daily work tool involves ruby which is an open-sourced language and rails an open-source framework. Starting with ruby, when I say I code in ruby, it also heavily involves using gems (aka libraries). As of today, there are 91,349 gems hosted on rubgems.org. That means I have at least 91,349 open-source projects to contribute on. And rails, I think contributing to rails is so easy, not because the code is simple to understand, it is not. But because of the community. Everyone helps you out if you are stuck at something. Here is an example:
I had sent a pull request with different commits. I had added example for squish method, remove method and a test case for that.
os1
In my diff, you can see there is an extra line added which is not needed. And Zachary Scott , a member of rails core team, asks me to remove that extra line.

Now, I also saw that extra line while submitting the pull request, but I just ignored it. Because I did not knew why that line came even after I have not added it. So I send him a screenshot of the code at my end that I have no extra line break.
os2

And then Eileen M. Uchitelle came to my rescue, who I have never met in person and she has nothing to do with the pull request that I have sent, but she still helped me out. See it here:
os3
So it was not a new line, but an extra space or a tab.

Now, another rails committer Vijay Dev gets involved with the PR, and asks me to squash all commits into 1 big commit. I did that and I sent a new PR with squashed commits. And then Abdelkader Boudih, who is also part of the rails core team, asks me to force push it. It is exactly the reason I sent a new PR because I wanted to avoid force push.
os4
And then he asks me to remove the “[ci skip]” tag from the commit. Because I have added a new test case. In case you do not know, if you have made changes like changing documentation, then there is no need to run the test cases for such PR. But I have a new test case, so I had to remove the ci skip tag.
os5

So, you can clearly see the mistakes I have made, and how everyone helped out. Here is the PR.

2. Am I good enough?
It is something that I still ask to myself. Am I good enough to contribute to somebody else’s project, may be I should work for 5 more years and then I will make contributions. But believe me, you are good enough to contribute to any project you like from the day you start writing that “hello world” program in your favourite language. The only regret that I have is why did I start so late … why it took me at least 2 years to make that first contribution. Because contributing to open-source projects is not just about sending a PR for a new feature that you wish to see or fixing a critical bug which nobody seems to solve. There is so much more to it. You can help out in documentation, you can add comments to methods explaining what they do or you can fix failing test case or add a new one which you think the author has missed.

One of the best ways to start contributing is by observing what kind of pull requests other developers send, and you will start finding a pattern in those which will help you send your first PR.

3. What will I gain from it?
Satisfaction, Recognition, Learning. These were the main reasons I started contributing in the first place.

I think these are the main hurdles I faced.I hope this blog post helps some of you and you can avoid the mistakes I have made. And it would be great, if you could share your experience of contributing to open-source projects. Mention it in comments or tweet me at jainrishi15 if you have a similar post and I will try to include it in the blog post.

UPDATE:
I came across this post by PJ Hagerty which is about how and why to contribute to ruby.

Migrating from ruby 1.9.3 to ruby 2.0.0

Since support for ruby 1.9.3 is suppose to end in 2015, it was high time I migrate from ruby 1.9.3 to ruby 2.0.0, and eventually to ruby 2.2.0. These are the steps I followed while migrating to ruby 2.0.0 and the problems I faced.

1. bundle install for ruby 2.0.0

2. run test cases.
Few test cases failed. The error message was:

NoMethodError:
  private method `initialize_dup' called at line:.. in file ....

When I looked at the line which was producing this error, it was

user.dup

It turns out this problem was fixed in Rails 3.2.13. So I upgraded the Rails version to 3.2.13. If you want to read more about, here is the github issue page.

3. Bumped up Rails to 3.2.13 in Gemfile.
4. bundle install again.

Rails locked at 3.1.11

So, did

bundle update rails

And, another error: devise_invitable(0.6.0) depends on Rails >= 3.0.0 and = 3.0.0 and < 4.0 .

So came step 5.
5. Bumped devise_invitable to 1.0.0 in Gemfile.

6.

bundle update devise_invitable.

7. Another error: devise_invitable depends on devise ~ 2.0

I was on a much lower version of devise. So I upgraded devise to 2.0.0 in Gemfile.

8.

bundle update devise

Another error popped up, devise 2.0.0 depends on railties ~3.1 and I had rails pointed to 3.2.13. Now growing with frustation, I did:

bundle update

and upgraded the whole stack and no error.. boom ..!!

9. Run test cases again and the previous test cases which were failing, passed, but new ones failed now. The error was:

Paperclip::Errors::MissingRequiredValidatorError

When I did `bundle update`, it updated paperclip to 4.2.0 and in this version, in your model you need to mention the content_type and file_name validation of the file or explicitly mention that you do not need these validations. This is how you can do it all:

validates_attachment_content_type :image, :content_type =&gt; [&quot;image/jpg&quot;, &quot;image/jpeg&quot;, &quot;image/png&quot;, &quot;image/gif&quot;]
validates_attachment_file_name :avatar, :matches =&gt; [/png\Z/, /jpe?g\Z/, /gif\Z/]

or

do_not_validate_attachment_file_type :image

You can read about it more on this stackoverflow thread.

10. So I changed my models accordingly.
11. ran test cases again and boom.. all test cases passing..!!

And finally rails server starts. But this does not mean I will not face any problems henceforth because I have 78% test coverage and this means there is considerable chunk of code which is not checked yet, this means a day of manual testing awaits me..!!

I will keep updating this post with new steps and problems I am facing. But until then,

12. Manual labour .. !!!

ActiveSupport – Array

In this blog post, I will write about some of the magical methods or tricks that ActiveSupport#Array provides us.

in_groups_of
The first method which blew my mind was ‘in_groups_of’. This method is used to split the array in groups of a number. Ex:

%w(1 2 3 4 5 6 7 8 9 10).in_groups_of(3) {|group| p group}

The result would be:

["1", "2", "3"]
["4", "5", "6"]
["7", "8", "9"]
["10", nil, nil]

This is pretty straight forward. The cool part is we can pass a parameter to fill the empty places with. By default it is nil. For ex:

%w(1 2 3 4 5 6 7 8 9 10).in_groups_of(3, 'my parameter') {|group| p group}

The result would be:

["1", "2", "3"]
["4", "5", "6"]
["7", "8", "9"]
["10", 'my parameter', 'my parameter']

And this is not it. It goes one step further. If we do this:

%w(1 2 3 4 5 6 7 8 9 10).in_groups_of(3, false) {|group| p group}

The result would be:

["1", "2", "3"]
["4", "5", "6"]
["7", "8", "9"]
["10"]

Surprised…!! It does not fill with any value (nil or false) when false is passed as the second parameter. This is pretty neat..!! You can read about it or if you want to see the source code for this method, read here( rails/active_support/lib/active_support/core_ext/array/grouping.rb)

to_sentence
Next up is ‘to_sentence’. to_sentence is used to convert the elements of an array to a sentence using word connectors. For ex:

['one', 'two'].to_sentence
# => "one and two"   

We can even pass the connector for the words. Ex:

['one', 'two'].to_sentence(two_words_connector: '-')
# => "one-two"
['one', 'two', 'three'].to_sentence(words_connector: ' or ', last_word_connector: ' or at least ')
# => "one or two or at least three"

And we can even pass which language to use for connections. ex:

['uno', 'dos'].to_sentence(locale: :es)

You can read more about it here, from the source code:
active_support/lib/active_support/core_ext/array/conversions.rb

These are pretty neat methods which ActiveSupport provides us. There are more methods which I could have mentioned, but if you want to find out more such methods the best way is to read the rails source code.