Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reload rubygems in irb?

Tags:

ruby

rubygems

irb

I've this script right now.

def r(this)
  require this
  puts "#{this} is now loaded."
rescue LoadError
  puts "The gem '#{this}' is missing."
  puts "Should I install it? [y/n]"
  data = gets
  if data =~ /yes|y/i
    puts "Installing #{this}, hold on."
    if `gem install #{this}` =~ /Successfully/i
      load this
    end
  else
    puts "Okey, goodbye."
  end
end

That makes it possible to require libs on the fly. Like this: r "haml".

The problem is that I can't load the gem after it has been installed. Using load this or load File.expand_path("~/.irbrc") does not work.

Here is an example.

>> r "absolutize"
The gem 'absolutize' is missing.
Should I install it? [y/n]
y
Installing absolutize, hold on
LoadError: no such file to load -- absolutize
>> require "absolutize"
LoadError: no such file to load -- absolutize
>> exit
$ irb
>> require "absolutize"
=> true

Is there a way to reload rubygems or irb on the fly?

like image 696
Linus Oleander Avatar asked Feb 02 '23 19:02

Linus Oleander


2 Answers

I did not try, but I think you might be looking for Gem.clear_paths

Reset the dir and path values. The next time dir or path is requested, the values will be calculated from scratch. This is mainly used by the unit tests to provide test isolation.

like image 77
Amadan Avatar answered Feb 05 '23 09:02

Amadan


You can reset irb by calling exec('irb')

like image 42
Mario Avatar answered Feb 05 '23 07:02

Mario