Lesson Weekend

Ruby is a powerful scripting language. Scripts can be used to automate tasks such as creating and searching files and managing and organizing subdirectories. Companies like GitHub, Chef and Puppet use Ruby for scripting. Devops teams use scripting to combine development and operations.

We’ll use Ruby as a scripting language before we dive into using popular Ruby-based web development frameworks such as Sinatra and Rails. We’ll use scripts mainly to increase our familiarity with Ruby techniques such as looping, writing methods and creating classes; if you find you’re particularly interested in scripting, you can explore further on your own.

Creating a Ruby Script


Let’s start by creating a "Hello World!" script in Ruby.

hello.rb
#!/usr/bin/env ruby
puts "Hello world!"

We can run this script in the command line by going to the directory where the file lives and typing in ruby hello.rb. The output of the program will be Hello world!.

Let’s take a look at the content of the script itself. The opening line is called a shebang because of the first two characters #!. This line provides the shell (in our case, bash) with the absolute path to the Ruby interpreter. If we were writing a script in another language, we’d provide the path to that interpreter instead. For instance, if our script was written in Python, our shebang would look like this: #!/usr/bin/env python.

Technically, our script would work even if we didn’t include a shebang (at least on the computers at Epicodus). This is because the shell will check the default path and find the interpreter anyway. Even so, our scripts should always begin with a shebang. We’ll see one of the reasons why in a moment. Note that you only need a shebang for script files that you'll need to run in the terminal. The majority of the Ruby files we'll create in this course will not be script files.

We can run our script without typing ruby in front of the file name (as long as we have the shebang). Try this out in the command line: ./hello.rb.

Windows users should get Hello World! returned just fine. But for Mac users...

...Whoops! You’ll get the following error: bash: ./hello.rb: Permission denied.

Mac users need to make the file executable by changing its permissions. You can make the script executable with the following command: chmod +x hello.rb. chmod is a shell command that allows us to change the permissions for a file. The +x specifies that the script should be executable. Now we can type ./hello.rb and get the output of our program!

Let’s take a look at the second line of our script:

puts "Hello world!"

puts is a method that’s available to all Objects; it’s mixed in from the Kernel module in Ruby. Remember how we used methods() to see that String, Integer and Float share some methods? Many of these methods are mixed in from Kernel. Specifically, puts will output a string to the terminal and then add a newline. (We could also use print, another Kernel method, which doesn’t add a newline.)

Let’s modify our script to use two more Kernel methods: gets and chomp. We’ll update the script so it responds with a personalized greeting.

hello.rb
#!/usr/bin/env ruby
puts 'What is your name?'
name = gets.chomp
puts "Hello #{name}!"

If we run our script in the terminal, we’ll get a prompt that asks for a name. Once we enter a name and hit return, we’ll get a personalized greeting in response.

Just as puts outputs a line to the terminal, gets accepts input from the terminal. chomp removes any trailing newlines.

We also use string interpolation to pass the value of the name variable into a string. Note the use of both single quotes and double quotes in the code above. It’s a best practice to use double quotes only when string interpolation is needed and single quotes everywhere else.

Using ARGV


Let’s look at another approach for passing arguments into a script. While gets works fine, responding to prompts in the command line can get tedious after a while. It would be nice to have the option to pass in an argument on the command line like this: ruby hello.rb Fuzzy.

In fact, we can do that. We can pass an argument into a program just as we pass an argument into a method. When we do so, the program has access to the argument (or arguments) through the ARGV array.

ARGV is short for "argument vector." It’s capitalized because it is a constant, though we can modify or update ARGV if we need to.

Let’s update our script to take advantage of ARGV:

hello.rb
#!/usr/bin/env ruby
name = ARGV[0]
puts "Hello #{name}!"

Now when we do the following: ruby hello.rb Fuzzy, we’ll get the following response in the terminal: Hello Fuzzy!

So how exactly does ARGV work? Let’s modify our script so it responds to several arguments:

hello.rb
#!/usr/bin/env ruby
first_name = ARGV[0]
last_name = ARGV[1]
age = ARGV[2]
puts "This is ARGV, the arguments array: #{ARGV}."
puts "Hello #{first_name} #{last_name}. You are #{age} years old!"

Now, let’s type the following into the terminal: ruby hello.rb Fuzzy Bear 2.

Here’s the output in the terminal:

This is ARGV, the arguments array: ["Fuzzy", "Bear", "2"].
Hello Fuzzy Bear. You are 2 years old!

As we can see, ARGV is an array that has all the arguments we included when we executed our script. One interesting thing to note: the arguments have all been converted to strings. If we want to do some kind of computation with numbers, we need to make sure to use to_i to convert the strings to integers. Another important thing to consider: what happens if we don’t pass any arguments into hello.rb? The terminal will output Hello . You are years old!

Because ARGV is an array, we can use any Array method on it. For instance, we could loop through the array (which is common) or even remove elements from it.

We’ve learned two ways to pass user input into a script:

  • We can use puts and gets to prompt a user to enter input and then pass the input into our script;
  • We can also add arguments when we execute the script and access them through the ARGV array.

You should experiment with both throughout the section.

We’ve also successfully written our first script, which accepts user input and outputs a string to the terminal. Our script includes a shebang as well as a few Kernel methods. While we’ve only covered the basics here, we’re ready to start writing our own scripts!

Shebang: Provides the shell with the absolute path to the interpreter. Here's an example shebang:

#!/usr/bin/env ruby

puts will output a string to the terminal.

gets takes input from the terminal.

chomp removes any space before and after the input.

ARGV: short for "argument vector." We can use it to pass arguments to a script in the command line like this: ruby hello.rb Fuzzy. The code below uses the ARGV array to extract the argument passed to the script:

name = ARGV[0]
puts "Hello #{name}!"

Lesson 10 of 10
Last updated August 7, 2022