Ruby: How to load a file into interactive ruby console (IRB)?
Using ruby 1.9.3 on Ubuntu 14.04, I am able to load files from the current directory into irb
with the following command line:
irb -I . -r foo.rb
where foo.rb
is the file I want to load from my current directory. The -I
option is necessary to add the current directory (.
) to ruby's load path, as explained in the ruby man page. This makes it possible to require
files from the current directory, which is what the -r
option to irb
accomplishes.
The key piece that wasn't obvious for me when I had this problem is the -I
option. Once you do that, you can call require 'foo.rb'
from within irb
for any files in the current directory. And of course, you can specify any directory you want, not just .
with the -I
option. To include multiple directories on the load path, separate them with a colon (:), e.g.:
irb -I foo/:bar/:baz/
This command will add the directories foo
, bar
, and baz
to ruby's load path.
The final alternative is to use the relative or absolute path to the file when using require
or -r
to load a file:
irb -r ./foo.rb
or from within irb
:
> require './foo.rb'