Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails + Gems (in general): How do gems work?

I've been using Rails for a while and have always used gems in my gemfile, but I never really understood how the functionality of gems that I install actually become available. Say I use the has_permalinks gem (http://haspermalink.org/). It provides a .generate_permalink! method for my Model. Where does this method get defined? How come just I can use this method all of a sudden just by installing the gem? Is there some sort of include/require/load to initialize the gem's code so that it becomes accessible to the rest of the application? Also, where is this code stored when I install the gem?

like image 803
bigpotato Avatar asked Apr 10 '13 18:04

bigpotato


People also ask

How do I use a gem in Rails?

To use RubyGems to install gems, all you have to do is type gem install [the name of the gem you want to install] . RubyGems will then go ahead and install that gem for you from its library.

What are gems in Rails?

Gems in Rails are libraries that allow any Ruby on Rails developer to add functionalities without writing code. You can also call Ruby on Rails gems as plugins for adding features. A Ruby gem enables adding features without creating the code again and again.

How do gems work Ruby?

Gems can be used to extend or modify functionality in Ruby applications. Commonly they're used to distribute reusable functionality that is shared with other Rubyists for use in their applications and libraries. Some gems provide command line utilities to help automate tasks and speed up your work.

How does gem install work?

What does gem install do? gem install , in its simplest form, does something kind of like this. It grabs the gem and puts its files into a special directory on your system. You can see where gem install will install your gems if you run gem environment (look for the INSTALLATION DIRECTORY: line):


1 Answers

I answered your questions separately, and out of order, but I think it actually might make it easier to understand the answers in this order.

Also, where is this code stored when I install the gem?

If you're using Bundler, you can do bundle show has_permalink and it will show you where that gem is installed. Here's an example of me doing it with the pg gem:

✗ bundle show pg
/Users/jasonswett/.rvm/gems/ruby-1.9.2-p320@jason/gems/pg-0.11.0

Where does this method get defined?

If you do the bundle show thing, it returns a path - the method is defined somewhere in there. (You can use grep -r 'def generate_permalink' /gem/path to find exactly where if you want.)

How come just I can use this method all of a sudden just by installing the gem? Is there some sort of include/require/load to initialize the gem's code so that it becomes accessible to the rest of the application?

Look at this part of the doc about the Rails initialization process: http://guides.rubyonrails.org/initialization.html#config-boot-rb

In a standard Rails application, there’s a Gemfile which declares all dependencies of the application. config/boot.rb sets ENV["BUNDLE_GEMFILE"] to the location of this file, then requires Bundler and calls Bundler.setup which adds the dependencies of the application (including all the Rails parts) to the load path, making them available for the application to load.

It looks like, fairly early on in the process, Rails looks at your Gemfile and loads all your gems via Bundler. So there's your include.

like image 109
Jason Swett Avatar answered Sep 30 '22 15:09

Jason Swett