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?
-
72I 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– ian2012-01-04 05:00:04 +00:00Commented Jan 4, 2012 at 5:00
-
19Fast forward to 2017 and now the OP has 2,555 points and over 100 badges. :)Daniel– Daniel2017-10-23 12:55:01 +00:00Commented Oct 23, 2017 at 12:55
-
I love the positive energy, but we've proved the point. Agreed that our community needs to be supportive to newcomers and to anyone needing support to learn a new skill.Hebron Watson– Hebron Watson2023-10-12 12:51:49 +00:00Commented Oct 12, 2023 at 12:51
10 Answers
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
6 Comments
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.cd to his Desktop directory, so that must be it.Open your terminal and open folder where file is saved.
Ex /home/User1/program/test.rb
- Open terminal
cd /home/User1/programruby test.rb
format or test.rb
class Test
def initialize
puts "I love India"
end
end
# initialize object
Test.new
output
I love India
3 Comments
Class should be class (with a lower c). Otherwise it won't work.Assuming ruby interpreter is in your PATH (it should be), you simply run
ruby your_file.rb
8 Comments
cd is a terminal command that changes current directory. For example, to go to a "Desktop" dir that is in your HOME_DIR, you'd have to do cd ~/Desktop.To call ruby file use : ruby your_program.rb
To execute your ruby file as script:
start your program with
#!/usr/bin/env rubyrun that script using
./your_program.rb param- If you are not able to execute this script check permissions for file.
1 Comment
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!
Comments
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