How to Execute a Ruby Script in Terminal

Rookie - Ruby: Run file in terminal

Ruby programs generally use the '.rb' extension, so in order to run a ruby file that you've written, you need to save it somewhere with that extension first- eg. 'my-app.rb'.

It's a good idea when starting out to save it in a folder inside your "Home" directory (/Users/your user name/). You can find that in the mac "Finder" by clicking on the folder on the left hand list that's named "your username". In your terminal, your home directory is shortened to '~/' - and you can easily change directory into it with that shortcut:

cd ~

While I've been learning, I've stuck to a quick, short directory to store my files- '~/code/'. Anything will do, but it's much quicker to type 'cd ~/code/my-app.rb' than to type something long like 'cd ~/Documents/Programming/Ruby/my-app.rb' every time. So when you're deciding on where to save, think about how much you'll have to type in terminal! :)

Once you've saved your file, and used 'cd' to change into the directory you've saved it in, you use the command 'ruby' to run it.

ruby my-app.rb

That's about all there is to actually running your file! There's so much more to using the terminal, and writing code- but there's plenty of info out there on how to start.

I found Chris Pine's "Learn To Program" really simple and easy to follow. There are plenty of other resources out there, too! Try out Try Ruby to get going straight in your browser.

Run ruby script in terminal with parameters

Save it to a file like this:

def add(*numbers)
numbers.inject(0) { |sum, number| sum + number }
end

result = add(*ARGV.map(&:to_i))
puts result

Then run it like ruby add_method.rb 4 6.

How Do I Create a Ruby App that Can Run Without Calling Ruby in the Terminal?

Make sure that your script has a correct shebang line, e.g. something like this:

#!/usr/bin/env ruby

Also, make sure that the user trying to run the script has read and execute permissions for the script file.

That is all you need to do. Assuming you have a Ruby execution engine installed, and its executable is named ruby and it is in your $PATH, then you can execute your script file like any other executable.

How to run Ruby scripts in Command Line?

You can put scripts in a file with a .rb extension and run it with:

ruby import_csv.rb

Your file import_csv.rb would contain:

#!/usr/bin/env ruby

require "jekyll-import";
JekyllImport::Importers::CSV.run({
"file" => "my_posts.csv"
})

I think the -rubygems switch is not needed since it is enabled by default. If not, add the line require rubygems before the other require line.

How to add a command to a ruby script for terminal in Kali linux?

That's called running a ruby script/program from the command line and passing "flags" like -c are passed to the script as command line arguments and are an array of string values separated by spaces normally.

Here's your very simple script:

tool.rb

#!/usr/bin/env ruby

if ARGV[0] == '-c'
puts 'blah blah'
end

You can run this from the command line exactly as you requested.

ruby tool.rb -c

Updated

If you need additional arguments or want to pass something else to your flag you can do as I mentioned ARGV is an array constructed from string passed after the name of your ruby script so:

#!/usr/bin/env ruby

if ARGV[0] == '-c'
puts "blah blah #{ARGV[1]}" # array items are called by index
end

So if you do this:

ruby tool.rb -c foo

You will get:

blah blah foo


Related Topics



Leave a reply



Submit