VocabReminder
Easily build a great vocabulary without studying! Now on the App Store!
Click here for more info about the app

Ruby Reload! Method in Non Rails IRB Sessions

       

I love the Rails reload! function when in the console. I need it in Irb. To get it back this is what I did. If you don't already have an .irbrc file in your home directory, just create it.


vim ~/.irbrc
or textmate if you prefer
mate ~/.irbrc

Add this little snippet to it...


unless defined?(reload!)
  $files = []
  def load!(file)
    $files << file
    load file
  end
  def reload!
    $files.each { |f| load f }
  end
end

Usage : To load files in irb just use the method we defined in the .irbrc file "load!" notice the bang "!". I don't want to overwrite the actual load method. This load! method will just put the file in an array before loading it, so when running reload! it will iterate over this collection and load them again with whatever changes have since taken place. The unless conditional is so that you don't overwrite the reload! method if you're actually in the rails console.

Tagged w/ #console #development environments #irb #rc files #rubyruby