Using the Command Line to Run Ruby Scripts

Running and Executing rb Files

Kids learning to code at home
Imgorthand/Getty Images

Before really starting to use Ruby, you need to have a basic understanding of the command line. Since most Ruby scripts won't have graphical user interfaces, you'll be running them from the command line. Thus, you'll need to know, at the very least, how to navigate the directory structure and how to use pipe characters (such as |, < and >) to redirect input and output. The commands in this tutorial are the same on Windows, Linux and OS X.

  • To start a command prompt on Windows, go to Start -> Run. In the dialog that appears, enter cmd into the input box and press OK.
  • To start a command prompt on Ubuntu Linux, go to Applications -> Accessories -> Terminal.
  • To start a command prompt on OS X, go to Applications -> Utilities -> Terminal.

Once you're at the command line, you'll be presented with a prompt. It's often a single character such as $ or #. The prompt may also contain more information, such as your username or your current directory. To enter a command all you need to do is type in the command and hit the enter key.

The first command to learn is the cd command, which will be used to get to the directory where you keep your Ruby files. The command below will change directory to the \scripts directory. Note that on Windows systems, the backslash character is used to delimit directories but on Linux and OS X, the forward slash character is used.

C:\ruby>cd \scripts

Running Ruby Scripts

Now that you know how to navigate to your Ruby scripts (or your rb files), it's time to run them. Open your text editor and save the following program as test.rb.

#!/usr/bin/env ruby

 

print "What is your name? "

name = gets.chomp

puts "Hello #{name}!"

Open a command line window and navigate to your Ruby scripts directory using the cd command.

Once there, you can list files, using the dir command on Windows or the ls command on Linux or OS X. Your Ruby files will all have the .rb file extension. To run the test.rb Ruby script, run the command ruby test.rb. The script should ask you for your name and greet you.

Alternatively, you can configure your script to run without using the Ruby command. On Windows, the one-click installer already set up a file association with the .rb file extension. Simply running the command test.rb will run the script. In Linux and OS X, for scripts to run automatically, two things must be in place: a "shebang" line and the file being marked as executable.

The shebang line is already done for you; it's the first line in the script starting with #!. This tells the shell what type of file this is. In this case, it's a Ruby file to be executed with the Ruby interpreter. To mark the file as executable, run the command chmod +x test.rb. This will set a file permission bit indicating that the file is a program and that it can be run. Now, to run the program, simply enter the command ./test.rb.

Whether you invoke the Ruby interpreter manually with the Ruby command or run the Ruby script directly is up to you.

Functionally, they are the same thing. Use whichever method you feel most comfortable with.

Using Pipe Characters

Using the pipe characters is an important skill to master, as these characters will alter the input or output of a Ruby script. In this example, the > character is used to redirect the output of test.rb to a text file called test.txt instead of printing to the screen.

If you open new test.txt file after you run the script, you'll see the output of the test.rb Ruby script. Knowing how to save output to a .txt file can be very useful. It allows you to save program output for careful examination or to be used as input to another script at a later time.

C:\scripts>ruby example.rb >test.txt

Similarly, by using the < character instead of the > character you can redirect any input a Ruby script may read from the keyboard to read from a .txt file.

It's helpful to think of these two characters as funnels; you're funneling output to files and input from files.

C:\scripts>ruby example.rb

Then there's the pipe character, |. This character will funnel the output from one script to the input of another script. It's the equivalent of funneling the output of a script to a file, then funneling the input of a second script from that file. It just shortens the process.

The | character is useful in creating "filter" type programs, where one script generates unformatted output and another script formats the output to the desired format. Then the second script could be changed or replaced entirely without having to modify the first script at all.

C:\scripts>ruby example1.rb | ruby example2.rb

The Interactive Ruby Prompt

One of the great things about Ruby is that it's test-driven. The interactive Ruby prompt provides an interface to the Ruby language for instant experimentation. This comes in handy while learning Ruby and experimenting with things like regular expressions. Ruby statements can be run and the output and return values can be examined immediately. If you make a mistake, you can go back and edit your previous Ruby statements to correct those mistakes.

To start the IRB prompt, open your command-line and run the irb command. You'll be presented with the following prompt:

irb(main):001:0>

Type the "hello world" statement we've been using into the prompt and hit Enter. You'll see any output the statement generated as well as the return value of the statement before being returned to the prompt.

In this case, the statement output "Hello world!" and it returned nil.

irb(main):001:0> puts "Hello world!"

Hello world!

=> nilf

irb(main):002:0>

To run this command again, simply press the up key on your keyboard to get to the statement you previously ran and press the Enter key. If you want to edit the statement before running it again, press the left and right arrow keys to move the cursor to the correct place in the statement. Make your edits and press Enter to run the new command. Pressing up or down additional times will allow you to examine more of statements you've run.

The interactive Ruby tool should be used throughout learning Ruby. When you learn about a new feature or just want to try something, start up the interactive Ruby prompt and try it. See what the statement returns, pass different parameters to it and just do some general experimenting. Trying something yourself and seeing what it does can be a lot more valuable then just reading about it!