281

I've set everything up that I need on my Mac (Ruby, Rails, Homebrew, Git, etc), and I've even written a small program. Now, how do I execute it in Terminal? I wrote the program in Redcar and saved it as a .rb, but I don't know how to execute it through Terminal. I want to run the program and see if it actually works. How do I do this?

Chris
  • 1,416
  • 18
  • 29
Tom Maxwell
  • 9,273
  • 17
  • 55
  • 68
  • 66
    I don't know who voted down your very first question, but I think that's harsh so I've voted it back up. Good luck with everything, it all seems so confusing at first but persevere and before long it'll be second nature, and then you'll be helping out other people on here. – ian Jan 04 '12 at 05:00
  • 17
    Fast forward to 2017 and now the OP has 2,555 points and over 100 badges. :) – Daniel Oct 23 '17 at 12:55
  • 5
    Skip 3 more years and he has 7,875 points and 120+ badges! – Hacconnoisseur Dec 18 '20 at 23:23
  • 2
    Skip two more years and he has 8,943 points and 130+ badges! – Anton Sep 29 '22 at 12:12

10 Answers10

299

Just call: ruby your_program.rb

or

  • start your program with #!/usr/bin/env ruby,
  • make your file executable by running chmod +x your_program.rb
  • and do ./your_program.rb some_param
Vince
  • 1,570
  • 3
  • 27
  • 48
Robin
  • 21,667
  • 10
  • 62
  • 85
  • 2
    When I call ruby your_program.rb, I get this: ruby: No such file or directory -- testapp.rb (LoadError) – Tom Maxwell Jan 04 '12 at 02:53
  • 1
    @TomMaxwell When he says `ruby your_program.rb`, you are supposed to substitute 'your_program.rb' with whatever the name is of your program. In this case, based on your error message, it is probably supposed to be `ruby testapp.rb`. Though @Robin is correct in that you need to be in the same directory as the Ruby file. – Joshua Cheek Jan 04 '12 at 04:11
  • 3
    I think he did, otherwise the error message would not be about testapp.rb. But based on his other comments, he didn't know how to `cd` to his Desktop directory, so that must be it. – Robin Jan 04 '12 at 04:17
  • 1
    how about if there're some require in .rb file? – LiangWang Apr 01 '15 at 23:50
  • 1
    @Chris Page, I actually had this problem by using the first way. I changed mod as you suggested and it worked! Thanks a ton! – nstein Jun 14 '15 at 08:08
  • @ChrisPage , if your using hashbang #! , then you don't need to make it .rb just leave it without extension – Mani Jun 24 '16 at 19:47
52

Open your terminal and open folder where file is saved.
Ex /home/User1/program/test.rb

  1. Open terminal
  2. cd /home/User1/program
  3. ruby test.rb

format or test.rb

class Test 
  def initialize
   puts "I love India"
  end
end

# initialize object
Test.new

output

I love India
ice cream
  • 2,444
  • 2
  • 17
  • 13
vijay
  • 531
  • 4
  • 2
24

Assuming ruby interpreter is in your PATH (it should be), you simply run

ruby your_file.rb
Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367
20

To call ruby file use : ruby your_program.rb

To execute your ruby file as script:

  1. start your program with #!/usr/bin/env ruby

  2. run that script using ./your_program.rb param

  3. If you are not able to execute this script check permissions for file.
Priti Biyani
  • 414
  • 5
  • 11
  • Generally to execute a script, you need to give execute permission. If that is not given, it would not work with above command. Check your permissions and then use the same command. – Priti Biyani May 04 '17 at 17:44
4

Just invoke ruby XXXXX.rb in terminal, if the interpreter is in your $PATH variable.

( this can hardly be a rails thing, until you have it running. )

Jokester
  • 5,501
  • 3
  • 31
  • 39
3

For those not getting a solution for older answers, i simply put my file name as the very first line in my code.

like so

 #ruby_file_name_here.rb

 puts "hello world"
Gokigooooks
  • 794
  • 10
  • 20
2

Although its too late to answer this question, but still for those guys who came here to see the solution of same problem just like me and didn't get a satisfactory answer on this page, The reason is that you don't have your file in the form of .rb extension. You most probably have it in simple text mode. Let me elaborate. Binding up the whole solution on the page, here you go (assuming you filename is abc.rb or at least you created abc):

Type in terminal window:

cd ~/to/the/program/location
ruby abc.rb

and you are done

If the following error occurs

ruby: No such file or directory -- abc.rb (LoadError)

Then go to the directory in which you have the abc file, rename it as abc.rb Close gedit and reopen the file abc.rb. Apply the same set of commands and success!

Piyush Deshmukh
  • 519
  • 1
  • 7
  • 14
2

In case someone is trying to run a script in a RAILS environment, rails provide a runner to execute scripts in rails context via

rails runner my_script.rb

More details here: https://guides.rubyonrails.org/command_line.html#rails-runner

Nikhil Sahu
  • 2,463
  • 2
  • 32
  • 48
1

Open Terminal

cd to/the/program/location
ruby program.rb

or add #!/usr/bin/env ruby in the first of your program (script tell that this is executed using Ruby Interpreter)

Open Terminal

cd to/the/program/location
chmod 777 program.rb
./program.rb
Akhilan
  • 69
  • 3
1

You can run ruby code just passing -e option

ruby -e 'x = Time.now; puts x;'

Output will be:

2022-06-22 15:55:06 +0500

Artem P
  • 138
  • 7