Apr 14

While Rails itself already provides a lot of libraries that facilitate us to develop great web applications, but often we still need other Ruby libraries. Lucky for us because Ruby has rubygems that has become the standard format for library distribution.

To tell Rails what are gems that required, we simply add code like this in environment.rb

config.gem "will_paginate"
config.gem "hpricot"
config.gem "fastercsv"

Here are some ways to use gem in a Rails project.

Install the gem in the system

This way the most frequently used for those new to using Rails. Simply install the gem with the command: gem install whateveryouneed or rake gems:install and you are good to go. In this way requires us to ensure that gem is required have been installed on the server that is used for deployment later.

This method is quite practical when practiced by a small team or if you only as a developer, but would rather fuss if your team consists of many developers because they have to ensure that the required gem is already installed on their system.

Freeze gems

By using the command rake gems: unpack, all the gems that is required by your application will unpack into vendor/gems directory. Although this method will increase your application code size, but this will ease deployment later because gems do not have to be installed in the system.

Compared to the first method before, this method is more practical for a team that consists of many developers because each developer does not have to worry about whether the gem is needed is already installed on their system or not.

Bundler

This is the latest method. Bundler uses the management approach of the Merb and can be used not only on Rails course.

For Rails 2.3, Rails overriding Bundler own gem by inserting some handling code to config/boot.rb and create a new file config/preinitializer.rb. Then you move config.gem declarations to a file named Gemfile that is stored in the root directory of the application. We can categorize the required gem appropriate environment. Make sure to include Rails Itself and at least one source.

source :gemcutter
gem "rails", "~> 2.3.5"
gem "sqlite3-ruby", :require => "sqlite3"

# bundler requires these gems in all environments
# gem "nokogiri", "1.4.2"
# gem "geokit"

group :development do
  # bundler requires these gems in development
  # gem "rails-footnotes"
end

group :test do
  # bundler requires these gems while running tests
  # gem "rspec"
  # gem "faker"
end

You should find more information on their website.

Just as open source itself, in the Rails world there are always some options in doing something, of course choices we make will be influenced by our own needs. So how about you? methods such as what you use in handling the required gem in Rails?

Leave a Reply

Security Code: