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.

Ruby Enterprise Edition and Passenger ./script/console production fails and instead returns Loading production environment (Rails 2.3.5) Rails requires RubyGems >= 1.3.2. Please install RubyGems and try again: http://rubygems.rubyforge.org

After installing Ruby Enterprise Edition, REE, and Passenger on Ubuntu you may see this error message when you run script/console for the first time

./script/console production
# =>
Loading production environment (Rails 2.3.5)
Rails requires RubyGems >= 1.3.2. Please install RubyGems and try again: http://rubygems.rubyforge.org

You then scratch your head and run

which gem
which ruby
which rails

to find that all appears to be in order. You have rubygems installed , ruby is installed and so is rails. You also find that each are pointing to the correct location, which is something like /usr/bin/gem -> /opt/ruby-enterprise-x.x.x.x/bin/gem, where x.x.x.x is the version of REE.

The problem isn’t however, with any of the above. The issue is with the location of irb. If you installed (like me) irb with apt-get install irb, then irb isn’t aware of your shiny new REE and ruby gems. It’s a simple fix however, unlink irb and symlink the /usr/bin/irb to REE’s irb like so…

rm /usr/bin/irb

And symlink it to the irb that REE has in bin

ln -s /opt/ruby-enterprise-x.x.x.x/bin/irb /usr/bin/irb

Now cd into your rails app and run

./script/console production

Output Logger and SQL to the Rails Console in Development Mode

If you want to take a look at the SQL being generated by active record while your using the console, you can either type this into the console when it loads

ActiveRecord::Base.logger = Logger.new(STDOUT)

Or you can add it to your environment so that it’ll be the default behavior
rails_root/config/environments/development.rb

#...
ActiveRecord::Base.logger = Logger.new(STDOUT)

It’s a nice way to keep you away of any expensive queries you may unknowingly be writing!

Starting the Rails Console in Production Mode

To specify which mode you’d like the rails console to boot up in, just provide the string without any flags.

./script/console production
./script/console test
./script/console development

If you’re on windows, remember the backslash “\” rather than forward “/” and I believe you’ll also have to feed the ruby interpreter as well, like so…

ruby script\console production