One thing that I like with ruby is it have a lot of English sounding keywords: not, and, or, unless, etc. However, you should be aware that these keyword might not be equivalent with their “traditional” counterpart.
Take `not` for example. We use `not` to negate a boolean, which we traditionally use bang (!) for. However, not has different precedence level from `!`. In ruby, `!` has the highest precedence, while not has one of the lowest precedence, like you can see in the ruby docs. This might have surprising effect.
!true && false
not true && false
In the first example,
! has higher precedence than `&&`, so it will evaluates as `(!true) && false`. However in the second example `not` has lower precedence than `&&`, so it will evaluates as `not(true && false)`.
a = true
b = not a
b = !a
Because of this subtle difference, I avoid using not and just use ! every time.
As a ruby programmer, we are treated with a rich ecosystem of library called the ruby gems.
Along the way, sometime you found a new need that can’t be fulfilled by your current gem choice. This happened to me last month with geocoder.
When this happens you have two choices:
1. Roll out your own solution from scratch
2. Patch the current gem so that it can solve your problem
From my case, I need to use Googe Places Search API for geocoding, that the geocoder didn’t support yet. I check that it is not difficult to patch the gem (not always the case) and my code base already uses the gem a lot, so I decided to patch the gem.
It might be confusing if this is the first time you want to patch a gem. How to do edit the files locally? Turns out it is quite easy.
First, fork the gem repository, and clone the fork to your local computer, for example to:
Then modify your project Gemfile to specify your local path:
gem 'geocoder', path: '~/gempatch/geocoder'
Run bundle and voila! your project is running the gem from local source. Any modification you made on the gem locally will be immediately reflected on your project. Once you are happy with your modification, save and commit your change, and push it to the gem fork on your repository.
Now you can set your project to use the gem from your repository, so it will work in production
gem 'geocoder', git: "git://github.com/waruboy/geocoder.git", branch: "place-search"
Oh, and please consider to create a Pull Request to the main repository of the gem. It’s a nice way to contribute to the community and also great to put your name out there. Just make sure to read if the gem have any contributing guidelines before doing so.
PS. Thanks a lot for Tyler Tringas from Storemapper for sponsoring my contribution work to the Geocoder gem.