0

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?

1
  • IRB is for testing and evaluation code as you develop but not for running code meant to be a program; It stands for "Interactive Ruby". It's not the Ruby interpreter at all. You don't say whether you're on Windows or a *nix-type system, but in general you use ruby /path/to/your/ruby_script.rb which tells the interpreter to load and run the code. Perhaps it'd be really useful for you to find and read a Ruby tutorial, which would cover this within the first few pages. Commented Dec 4, 2013 at 3:38

4 Answers 4

2

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

ruby some_path/some_script.rb
Sign up to request clarification or add additional context in comments.

2 Comments

It says that it doesnt recognise Ruby. Does this mean I have not installed it properly?
No, the fact it doesn't recognize Ruby doesn't mean it's not installed right, it might mean you don't have Ruby in your path. What OS are you on? How did you install Ruby? Those questions and their answers should probably be a separate question.
1
require 'my_script.rb'

No special directory is required.

2 Comments

Sorry, it just says cannot load such file and from: c:/Ruby200/lib/ruby/2.0.0/rubygems/core_ext/kernal_require.rb:53:in'require'
You may have to specify the full path and make sure your editor didn't add a ".txt extension. (windoze will hide extensions from you unless you turn it on)
1

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 >

Comments

1

eval(File.read 'your_script.rb')

no special directory, just make sure to use correct path

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.