I am writing a Ruby script for use in the Rails environment, but I chose to run it from irb because reloading the Rails console can be a pain. Now the wait time is much shorter from irb, but I'm bothered that I have to restart irb and require the script everytime I make a change. Is there a simpler way of reloading a script from irb?
I found a method in this thread, but that only applies to gem files apparently. My require statement looks like this
require "#{File.expand_path(__FILE__)}/../lib/query"
EDIT: Having tried load
rather than require
, I still couldn't get it to work. I can't get a stop on these errors.
ruby-1.9.2-p0 > load "#{File.expand_path(__FILE__)}/../lib/query.rb" LoadError: no such file to load -- /Users/newuser/Dropbox/Sites/rails/hacknyc/(irb)/../lib/query.rb
If you only need to load one file into IRB you can invoke it with irb -r ./your_file. rb if it is in the same directory. This automatically requires the file and allows you to work with it immediately. If you want to add more than just -r between each file, well that's what I do and it works.
Reload: This command will allow you to make changes to your code, and continue to use the same console session without having to restart. Simply type in the “reload!” command after making changes and the console will reload the session.
In irb, File.expand_path(__FILE__)}
will just return "#{path you ran irb from}/(irb)"
. Which creates a path that doesn't actually exist. Luckily all file paths are relative to where you ran irb anyway. This means all you need is:
load "lib/query.rb"
If you want to use the __FILE__
in an actual file, that's fine, but don't expect it to produce a valid path in irb. Because an irb there is no "file" at all, so it cannot return valid path at all.
Also, __FILE__
will work fine if used in a file loaded into irb via load or require. Cause that's kinda what it's for.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With