Posts for Tag: ruby

Choosing a Ruby gem

Instead of writing yet another list of gems you should be using, i'll try to explain how i choose a gem for a particular task or function. This is by no means a complete checklist, it's just my typical way of going about things.

Rubygems

If you're a Ruby developer you are probably familiar with the website rubygems.org. It's "the" repository for all information about Ruby gems.

Things i find important when looking for a gem:

Number of total downloads

Though it will not tell you whether a gem is any good, it does tell you whether a lot of people looked at it as a good candidate for the job you need done. It's also an indicator for the community you can expect around this gem (helps when looking for tutorials, documentation, and general help).

Release date of the last versions

This will show you how frequently is the gem updated, which is useful to know how fast are fixes and new features introduced.

Sometimes it can happen that a really good gem hasn't had a release in a while. For example a couple of weeks ago i was looking for a gem that wrapped the Marvel Comics API. If you look for gems for that you find that most are really old. Doesn't mean they don't work properly, it just means that the API hasn't changed since and they are still relevant. When that's the case, i usually go for the one that was released last, just because the author had the gift of hindsight over the other ones and might have done a better, more polished job at it. Not always true, but... :)

Dependencies and License

To a lesser degree, i also take a look at these.

The less dependencies a gem has the lighter your code will be. Again, not always true, but it's a good rule of thumb. Also pay attention if the license is compatible with your own project.

    If a particular gem feels like a good candidate, I then visit its...

    ...Github/Repository/Website

    There, i usually check for a couple of things:

    Documentation

    This is how you learn how to use the gem, so it helps if the docs are really good (and maintained!).

    If it's an opensource project (which most gems are), then i also look for:

    Last commit date

    A clear indicator of the health of the gem. Also, take into consideration what i wrote earlier about gems that don't update often: it might be because they just don't need to.

    Number of contributors, commenters, discussions

    They show how active the community is around this gem. If big, it helps later when trying to find information how to do things if you get stuck, for example.

    Number of open issues (and resolved)

    If you take a glance at the last issues, when they were created, when they were replied to or resolved, this will also give you a good idea of how active the developers are, and how fast can you expect a response if you report something.

    Code

    Looking at the source code, though not as important as the rest, can also provide some insight to how performant, optimized the gem is at the job it does. It might also help you figure out if will be easy to monkey patch something to fit your needs later. And who knows, you mighty also learn something new from it :)

    Conclusion

    Though not (by any means) a complete guide for choosing a gem, this might be a good starting point. Also, some of these "metrics" might apply for choosing other software and code libraries.

    Like most things in life, your mileage may vary.

    Happy coding!

    Problem starting Sidekiq in development

    If you ever get this error:

    can't link outside actor context

    Followed by something like:

        /Library/Ruby/Gems/2.0.0/gems/celluloid-0.16.0/lib/celluloid.rb:176:in `new_link'
        /Library/Ruby/Gems/2.0.0/gems/sidekiq-3.3.4/lib/sidekiq/launcher.rb:21:in `initialize'
        /Library/Ruby/Gems/2.0.0/gems/sidekiq-3.3.4/lib/sidekiq/cli.rb:81:in `new'
        /Library/Ruby/Gems/2.0.0/gems/sidekiq-3.3.4/lib/sidekiq/cli.rb:81:in `run'
        /Library/Ruby/Gems/2.0.0/gems/sidekiq-3.3.4/bin/sidekiq:8:in `<top (required)>'
        /Library/Ruby/Gems/2.0.0/bin/sidekiq:23:in `load'
        /Library/Ruby/Gems/2.0.0/bin/sidekiq:23:in `<main>'

    It's mostly likely somehow related with either ZenTest or another testing framework. 

    In my case, i was adding the ZenTest gem both in the development and test groups of my Gemfile. Moving it away from the development group solved the problem.

    'Error compiling pg Ruby gem on OSX'

    If you get this error (or similar) while trying to run **bundle install** on one of your Ruby projects or trying to install the **pg** gem:

    Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.

    /Users/void/.rvm/rubies/ruby-2.0.0-p0/bin/ruby extconf.rb --with-iconv-include=/usr/local/opt/libiconv/include --with-iconv-lib=/usr/local/opt/libiconv/lib checking for pg_config... no No pg_config... trying anyway. If building fails, please try again with --with-pg-config=/path/to/pg_config checking for libpq-fe.h... no Can't find the 'libpq-fe.h header *** extconf.rb failed *** Could not create Makefile due to some reason, probably lack of necessary libraries and/or headers. Check the mkmf.log file for more details. You may need configuration options.

    The solution is to install the gem like this:

    sudo env ARCHFLAGS="-arch i386" gem install pg -- --with-pg-include=/Library/PostgreSQL/8.4/include/ --with-pg-lib=/Library/PostgreSQL/8.4/lib/

    **Note:** Replace 8.4 with your installed PostgreSQL version in the above command.

    Date and time in ruby

    Time.now will give you the current date (and time), like so:

    Time.now 2012-11-22 22:35:01 +0000

    You can also request all the date parts individually:

    Time.now.hour 22 Time.now.min 35 Time.now.sec 01 Time.now.day 22 Time.now.month 11 Time.now.year 2012

    Cool (and easy), right? If you want to change a date, you can add seconds to it:

    t = Time.now 2012-11-22 22:35:01 +0000 t2 = t + 10 # 10 Seconds 2012-11-22 22:35:11 +0000 t3 = t + 10*60 # 10 minutes 2012-11-22 22:45:01 +0000 t4 = t + 10*60*60 # 10 hours 2012-11-23 08:35:01 +0000

    If you happen to be developing a Ruby on Rails application then it gets even easier:

    t = Time.now 2012-11-22 22:35:01 +0000 t2 = t + 1.hour 2012-11-22 23:35:01 +0000

    That&#39;s right, in Rails numerals get some extra methods injected into them, like hour, hours, minute, minutes, second, seconds, day, days, and so on... And people still ask me why i love Ruby (and Rails)... :)