Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
workaround to load irb specific gem (loaded in .irbrc) in bundler environment, like rails3 console
# Add all gems in the global gemset to the $LOAD_PATH so they can be used in rails3 console with bundler
if defined?(::Bundler)
$LOAD_PATH.concat Dir.glob("#{ENV['rvm_path']}/gems/#{ENV['rvm_ruby_string']}@global/gems/*/lib")
end
@skojin

This comment has been minimized.

Copy link
Owner Author

commented Jan 25, 2011

use as require_without_bundler 'awesome_print', 'hirb', 'interactive_editor'

@lucapette

This comment has been minimized.

Copy link

commented Jan 28, 2011

Nice, i'll give it a try :)

@raggi

This comment has been minimized.

Copy link

commented Apr 26, 2011

oh c'mon.

module Bundler
  # Activate a gem from outside the Gemfile. Could be considered harmful.
  # Might be useful for .irbrc and friends. It's a slow activation, but
  # after activation, files from the activated gems will be available for
  # normal require.
  def self.activate_outside(*gems)
    # Bundler doesn't cripple this:
    Gem.source_index.refresh!
    # Or this:
    Gem.activate(*gems)
  ensure
    # Re-enable the bundler lockdown via bundlers #initialize hack
    Gem.send(:class_variable_set, :@@source_index, nil)
  end
end
@lucapette

This comment has been minimized.

Copy link

commented Apr 29, 2011

Your solution looks wonderful. Thank you for sharing. I'll update my irbrc.

@mschulkind

This comment has been minimized.

Copy link

commented May 12, 2011

Here's an alternative that's working for me, just stick this in your .irbrc :

# Add all gems in the global gemset to the $LOAD_PATH so they can be used even
# in places like 'rails console'.
if defined?(::Bundler)
  global_gemset = ENV['GEM_PATH'].split(':').grep(/ruby.*@global/).first
  if global_gemset
    all_global_gem_paths = Dir.glob("#{global_gemset}/gems/*")
    all_global_gem_paths.each do |p|
      gem_path = "#{p}/lib"
      $LOAD_PATH << gem_path
    end
  end
end
@skojin

This comment has been minimized.

Copy link
Owner Author

commented May 13, 2011

@mschulkind your solution is better, but I'm not found GEM_PATH, in my ENV, but found ENV['rvm_ruby_global_gems_path'].
I'm updated gist with new version

@IslamAzab

This comment has been minimized.

Copy link

commented Feb 23, 2015

@skojin I tried your solution but it didn't work for me. ENV['rvm_ruby_string'] is empty.

@mschulkind's alternative worked fine for me.

@Heath101

This comment has been minimized.

Copy link

commented Jul 27, 2017

@mschulkind Thanks for posting that snippet. I've shortened it a bit, to make it a bit easier to grok:

# Add all gems in the global gemset to the $LOAD_PATH so they can be used even
# in places like 'rails console'.
if defined?(::Bundler)
  global_gemset = ENV['GEM_PATH'].split(':').grep(/ruby.*@global/).first
  $LOAD_PATH.concat(Dir.glob("#{global_gemset}/gems/*/lib")) if global_gemset
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.