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?

asked Dec 4, 2013 at 2:01
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
answered Dec 4, 2013 at 2:06
2
  • It says that it doesnt recognise Ruby. Does this mean I have not installed it properly? Commented Dec 4, 2013 at 2:22
  • 1
    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. Commented Dec 4, 2013 at 3:40
1
require 'my_script.rb'

No special directory is required.

answered Dec 4, 2013 at 2:06
2
  • 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' Commented Dec 4, 2013 at 2:15
  • 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) Commented Dec 4, 2013 at 4:00
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 >
answered Dec 4, 2013 at 2:36
0
1

eval(File.read 'your_script.rb')

no special directory, just make sure to use correct path

answered Sep 28, 2018 at 23:24

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.