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
4 Answers 4
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
-
It says that it doesnt recognise Ruby. Does this mean I have not installed it properly?MeltingDog– MeltingDog2013年12月04日 02:22:35 +00:00Commented Dec 4, 2013 at 2:22
-
1No, 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.the Tin Man– the Tin Man2013年12月04日 03:40:06 +00:00Commented Dec 4, 2013 at 3:40
require 'my_script.rb'
No special directory is required.
answered Dec 4, 2013 at 2:06
-
Sorry, it just says
cannot load such file
andfrom: c:/Ruby200/lib/ruby/2.0.0/rubygems/core_ext/kernal_require.rb:53:in'require'
MeltingDog– MeltingDog2013年12月04日 02:15:52 +00:00Commented 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)Daniel– Daniel2013年12月04日 04:00:41 +00:00Commented Dec 4, 2013 at 4:00
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
eval(File.read 'your_script.rb')
no special directory, just make sure to use correct path
answered Sep 28, 2018 at 23:24
lang-rb
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.