How to run a .rb file from IRB?

Katrina picture Katrina · May 27, 2011 · Viewed 65.9k times · Source

I am starting out with Ruby on Rails. I am currently going through a tutorial where it says that I have to run a .rb file from IRB and that that will create a .xml file in my current directory.

My question is how do I run a .rb file in IRB?
And do I have to be in the directory where this .rb file lives when I run it in IRB?

I tried the following: just typing irb on the command line in the directory of the file. That starts an IRB session as far as I understand.
Then I typed irb "filename.rb" which went through but didn't create anything in the current directory but at least it didn't give any errors.

I also tried a whole bunch of other stuff that plain gave me errors. So I don't think I can solve this myself and googling the matter didn't help at all.

I am running Leopard.

Answer

J-_-L picture J-_-L · May 27, 2011

You can "run" a file in irb by just requiring or loading it.

$ irb
>> load './filename.rb'

To change your current working directory within irb, you can use FileUtils:

>> require 'fileutils'
>> FileUtils.pwd # prints working directory
>> FileUtils.cd '/path/to/somewhere' # changes the directory