MeltingDog
MeltingDog

Reputation: 15404

How to run external script with Ruby?

I am new to Ruby and am learning from this tutorial on the Ruby site.

I can run simple scripts from the IRB command line but I am not sure how to run a script I have written on an external .rb file.

Is there a special directory I must put this in to run it from IRB?

Upvotes: 0

Views: 603

Answers (4)

Matthias Michael Engh
Matthias Michael Engh

Reputation: 1249

eval(File.read 'your_script.rb')

no special directory, just make sure to use correct path

Upvotes: 1

Javid Jamae
Javid Jamae

Reputation: 8999

Use require './filename.rb'. For example:

06:34:38 ~$ echo "puts 'asdf'" > foo.rb
06:34:55 ~$ irb
2.0.0p247 :001 > require './foo.rb'
asdf
 => true
2.0.0p247 :002 >

Upvotes: 1

mechanicalfish
mechanicalfish

Reputation: 12826

You should not run scripts in files using IRB. Exit the IRB and run:

ruby some_path/some_script.rb

Upvotes: 2

Daniel
Daniel

Reputation: 7172

require 'my_script.rb'

No special directory is required.

Upvotes: 1

Related Questions