rubygems snippets

Find the installation directory of a RubyGem package programmatically

Tagged ruby, rubygems, gem, path, package  Languages ruby

This snippet prints the full path to the Rails installation directory--the path is retrieved using the RubyGems API:

require 'rubygems'
puts Gem.cache.search('rails').first.full_gem_path
# Specify version number
puts Gem.cache.search('rails', '=1.0.0').first.full_gem_path

Example output:

/usr/lib/ruby/gems/1.8/gems/rails-1.0.0

To find the directory where your gems are hiding, use:

gem env

There's also the gem which command:

$ gem which activesupport
(checking gem activesupport-2.3.2 for activesupport)
/opt/ruby-enterprise-1.8.6-20090201/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/activesupport.rb

Install missing gem executables with `bundle install`

Tagged bundler, rails, rubygems, ruby  Languages bash

When:

$ bundle exec cucumber

bundler: command not found: cucumber
Install missing gem executables with `bundle install`

Do:

$ rm .bundle/config # remove a broken bundler configuration
$ gem update --system # might help if rubygems is outdated / broken
$ gem update bundler # might help if bundler is outdated / broken
$ bundle install # dependency hell

Or, start using, e.g., golang, crystal, etc.