How to execute a Ruby script in Terminal? - ruby

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?

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

Open your terminal and open folder where file is saved.
Ex /home/User1/program/test.rb
Open terminal
cd /home/User1/program
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

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

To call ruby file use : ruby your_program.rb
To execute your ruby file as script:
start your program with #!/usr/bin/env ruby
run that script using ./your_program.rb param
If you are not able to execute this script check permissions for file.

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. )

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"

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!

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

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

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

Related

I can't run my Ruby program and I see errors in the terminal. I can't find the program

Good afternoon! I have a problem with Ruby. I downloaded this program from the official site https://www.ruby-lang.org/en/downloads/ on Fedora without any problems (using the command via the terminal). However, I cannot use the 'irb' command in the terminal to run Ruby on Linux as described here https://www.ruby-lang.org/en/documentation/quickstart/. Therefore, I cannot learn the Ruby programming language.Command 'irb' not found
I tried my best to run Ruby in other ways. One of them, as is customary in Fedora, is to write the name of the program in the terminal. But it opens a folder in which, I assume, Ruby is located (I can't find the folder itself either in Files or via the 'cd' command in the terminal). I kind of can open this folder in the terminal, but I can't find it in Files.
Oh, by the way, the 'irb' command doesn't work there. Only some real command for a program like this one will work: irb(main):001:0> "Hello World" (from https://www.ruby-lang.org/en/documentation/quickstart/). The program is and is not at the same time
Move on. I made a text document, wrote irb(main):001:0> "Hello World" in it (as written in "Ruby in Twenty Minutes"), and saved the file as hello.rb . Opened the hello.rb file path through the terminal and wrote 'ruby hello.rb' (without quotes, of course). Result: syntax error. hello.rb file Tried to open hello.rb via ruby in terminal
Oh yeah, I almost forgot: when I tried to open hello.rb in Files through another application, there was no Ruby script in the list (ruby or irb or at least something through which one could open a .rb file). No program for .rb format
I hope I have explained the problem in sufficient detail and clearly. And I hope for your help and understanding! In any case, good mood to you!
On Fedora & CentOS, the Ruby installation is split into many smaller packages. If you want to run irb, you'll also have to install the ruby-irb package using dnf install ruby-irb.

Terminal issue - ruby: no such file or directory

Total newb here. I'm trying to get a simple ruby program to run in the terminal on my MacBook Pro. I used Atom text editor to write the following:
class Sample
def hello
puts "Hello, World!"
end
end
s = Sample.new
s.hello
I saved the file as my_program.rb to a folder on my desktop. I go to the terminal to run the program. I type
ruby my_program.rb
and it returns
ruby: No such file or directory -- my_program.rb (LoadError)
I can use the irb and run a single line of ruby using
ruby -e 'puts "hello world"'
But can't get it to find the .rb file.
I appreciate any help y'all can offer! Thanks!
ruby ~/Desktop/my_program.rb
Ruby might be clever, but is has no mind-reading builtin. You need to tell it, where your file is. This is, of course, not Ruby-specific, but applies to all commands - they can't guess, where in your file system you have stored a file.
An alternative would be to place ~/Desktop in your PATH and use
ruby -S my_program.rb
Ruby will then execute the first program with this name which it finds in $PATH. Whether it is wise to place the Desktop directory into the PATH is a different issue....

Run .rb (Ruby) file on a Shell Script

I created a shell script to automate some processes, something like that:
#!/bin/bash
ruby RubyFile.rb
But when I run this script, I get this error:
ruby(2882): Operation not permitted
Any one knows what the hell is this?
I use this when I want want to run Ruby code "as executable".
#!/usr/bin/env ruby
And then chmod the script.
chmod +x script.rb
And run it
./script.rb
I suggest usage of env because running a command through /usr/bin/env has the benefit of looking for whatever the default version of the program is in your current environment.
Why so complicated? Why not just
#!/usr/bin/ruby
or wherever your ruby is?

RVM on Ruby Scripts

I need for a Ruby script to be run using an rvm-selected version. I cannot change how the script is invoked, but I can modify the script. The script starts with:
#!/usr/bin/env ruby
Now, based on some information I found (in this question, for example), I tried this:
#!/usr/bin/env rvm-shell ree-1.8.7-2012.02#gitorious
But this only gives me this error message:
/usr/bin/env: rvm-shell ree-1.8.7-2012.02#gitorious: No such file or directory
Now, rvm is available, because this works (but doesn't bring the required ruby/gemset):
#!/usr/bin/env rvm-shell
I've tried this as well:
#!/usr/local/rvm/bin/rvm-shell ree-1.8.7-2012.02#gitorious
But this doesn't bring in the environment ("gem", which is only installed inside that gemset, is not available, for example). If I run that on the command line itself, it does open a shell with the proper environment.
So, has anyone done something like this? How can I fix it?
Does this work?
#!/location/of/rvm/folder/rubies/ree-1.8.7-2012.02#gitorious/bin/ruby

Is there a way to convert ruby scripts to executable(linuxmint)

Hey guys, I run LinuxMint and I have some scripts which I want to run without terminal, can you tell me how?
You need to:
Put as the first line of your ruby script the following shebang line (so bash knows what program to use to run this file)
#!/usr/bin/env ruby
Make your ruby file executable. In a console you can do this:
chmod 700 my_ruby_file.rb
Test that your ruby file is now executable. In a console write:
./my_ruby_file.rb
Notice that we are no longer calling "ruby my_ruby_file.rb", but instead just call the file directly "./my_ruby_file.rb"
If your ruby program executed normally, then everything is going well. If it does not execute and shows you an error, then something is not well done from the previous steps (and post your error)
In your desktop, create an application link, and point it to the my_ruby_file.rb
Once you have the application icon created and shown in your desktop, you can doble-clickit and it will run your application.
Tell us how it went- cheers

Resources