Skip to content

MR02 First Steps

Dave Strus edited this page Feb 1, 2016 · 6 revisions

First Steps

In this unit, we'll do the following:

  • Create a "hello world" style Ruby script
  • Make a script executable

We'll learn about the following concepts or tools:

  • Ruby interpreter
  • Shebang interpreter directives
  • UNIX: chmod

Creating an executable Ruby script

Carl Sagan said, "If you wish to make an apple pie from scratch, you must first invent the universe."

Similarly1, if you wish to make a command-line script that compiles unstoppable teams of extraordinary mutants, you must first know how to create a command-line script, period.

Create a "hello world" script

Let's make the simplest Ruby script we can.

Create a new directory for your project.

$ mkdir roster
$ cd roster

Create a roster.rb file with a simple "hello world" statement.

$ echo "puts 'Hello, mutants!'" >> roster.rb

To run the script, we have to pass it as an argument to the Ruby interpreter.

$ ruby roster.rb

That's too much typing! Let's make the script directly executable.

Making it executable

If you've ever looked at UNIX shell scripts, you may have noticed that they start with a line that begins with the characters #!, commonly called a shebang. That line tells the shell where to find the interpreter for the script.

We could include the direct path to ruby, like so:

#!/usr/bin/ruby

But hardcoding the path to the interpreter would break under several circumstances. It would cease to work if we ran the script on a machine where the Ruby interpreter were in a different location. If we use a tool to help us manage multiple versions of Ruby on the same machine, the hardcoded path would not be guaranteed to give us the correct Ruby version for our script.

Instead, we can use env to find the interpreter's location for us. Add the shebang interpreter directive to the beginning of roster.rb. It must be the very first line in the file.

roster.rb

#!/usr/bin/env ruby
puts 'Hello, mutant collector!'

That line means something if we use our shell (terminal) to execute it. Ruby treats the directive as a comment, thanks to the # character.

The script still isn't executable though. Back in the terminal, have a look at the access permissions for roster.rb with the ls -l command:

$ ls -l roster.rb
-rw-r--r--  1 dstrus  staff  43 Jan 28 20:52 roster.rb

The first ten characters that print, -rw-r--r--, indicate the access permissions for the file roster.rb. Right now, it is not executable.

Read more about Unix permissions.

We have to use chmod to add execute permissions:

$ chmod +x roster.rb

Read more about chmod.

Now run ls -l again.

$ ls -l roster.rb   
-rwxr-xr-x  1 dstrus  staff  43 Jan 28 20:52 roster.rb

Those xs mean that it's executable now.

UNIX will not look in the current directory for executables (unless that directory happens to be in your PATH), so we'll have to explicitly tell it the path when we run it.

$ ./roster.rb
Hello, mutant collector!

. can be used to refer to the current directory, much like .. refers to the parent directory, and ~ refers to the current user's home directory.

It's executable now, but it doesn't do much. Still, this might be a good time to put it under source code management.

1This is not very similar, but quoting profound thinkers makes us seem profound.