This log covers RubyGems package manager and Ruby Gem. Ruby 1.9 and above ships with RubyGems.
RubyGems is a package manager for Ruby that provoides a standard format for distributing Ruby programs and libraries, a tool designed to easily manage the installation of gems, and a server for distributing them.1
RubyGems2 is similar to
npm in functionality. It helps to manage Ruby load path (modifies the load path which controls how Ruby code is found by the
gem install do?
Gem is code zimpped up with a little extra data (meta data).
gem install do the following tasks3,
- Grabs the gem from the source
- Puts files into special directory (use
gem environmentto inspect the path)
Gem installation paths vary from systems.
How does Gem code get required?
Comments from RubyGems sources code
## # When RubyGems is required, Kernel#require is replaced with our own which # is capable of loading gems on demand. # # When you call <tt>require 'x'</tt>, this is what happens: # * If the file can be loaded from the existing Ruby loadpath, it # is. # * Otherwise, installed gems are searched for a file that matches. # If it's found in gem 'y', that gem is activated (added to the # loadpath). # # The normal <tt>require</tt> functionality of returning false if # that file has already been loaded is preserved.
Gem is like a library or plug-in. It is a simple way to distrubute functionalities.4
Gem contains meta data includes,
Gem contains components,
- Code (tests & utilities)
- Gemspec (meta data mentioned above)
A typical gem file structure,
% tree freewill freewill/ ├── bin/ │ └── freewill ├── lib/ │ └── freewill.rb ├── test/ │ └── test_freewill.rb ├── README ├── Rakefile └── freewill.gemspec
Two types of gems,
- Stand-alone Ruby program, e.g.
- Package like gem, e.g.
maruku, a gem convert Markdown to HTML
First type of gems, you can use them in command line like other command line program. For second type of gem, you use
require to load the gem code.
require 'maruku' str = '# Level 1 Heading' md = Maruku.new(str) md.to_html_document
require is a kernel method. It looks in the
$LOAD_PATH (arraoy of absolute paths) global variable.5
Ruby Gems are just source code heirarchies insatlled in a directory that ends up in
In order to require the gem/file, all we need to do is to add the file into the
lib = File.expand_path("../../lib", __FILE__) $:.unshift(lib)
$: is equivalent to
$: == $LOAD_PATH # -> true
__FILE__, stores the relative path to the current file.
Code snippet below is how Gem (yelp-ruby/yelp.gemspec) add itself into
# coding: utf-8 lib = File.expand_path('../lib', __FILE__) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) require 'yelp/version'
loadto execute code, and you use
requireto import libraries. 6
require a lib/gem/file second time does now work, because it prevent redeclaring constants.
Metaprogramming Ruby, https://pragprog.com/book/ppmetr2/metaprogramming-ruby-2 ↩