Running Ruby
Exercises
Objective
Now that we have Ruby installed, it's time to start writing some Ruby code!
At the end of this exercise, you'll know how to run Ruby code two different ways:
interactively with irb
and by creating Ruby program files. Here's where
the fun begins...
1. Run Ruby Interactively
An irb
session is a great place to start experimenting with Ruby
code. It gives you instant feedback.
-
Fire up an
irb
session from your command line:irb
Once it starts, you'll see a prompt where you can type in any Ruby code. Here's what our
irb
prompt looks like:>>
If your prompt looks slightly different than ours, don't worry about it. For example, you may see something like this:
irb(main):001:0>
Windows Users: You may also see the warning "DL is deprecated, please use Fiddle". Just ignore it.
However your prompt looks, this is where you type in Ruby expressions for
irb
to evaluate. -
Not surprisingly, Ruby is pretty good at math. So a really simple use of
irb
is as a calculator. Type in the following expression that adds two numbers together, then press Return:>> 1 + 2
Notice when you hit Return, the Ruby expression you typed in is evaluated and the result is printed out on the subsequent
irb
line after=>
, like this=> 3
-
Now it's your turn to have a go at
irb
(and test your math skills). Type in a Ruby expression to compute the number of hours in a calendar year. -
Next, create the string literal "Hello, Ruby!".
-
Assign that string to a variable named
greeting
. -
Finally, use the
upcase
method to print the greeting in all upper case letters. -
Go ahead, play around with a few more strings until you're comfortable running Ruby code interactively in
irb
.Tip: You can use the up and down arrow keys on your keyboard to navigate through the history of commands you've typed into
irb
. For example, use the up arrow to re-run the expression that prints the greeting in uppercase letters. -
When you're done, exit the
irb
session by typing, wait for it...exit
(orCtrl-D
):>> exit
So when you're in an irb
session, the Ruby interpreter runs
in a read-eval-print loop (REPL). It reads the expression you type,
evaluates the expression, and then prints the result. That's very handy!
2. Write a Ruby Program
While irb
is good for experimenting with Ruby in an interactive
environment, there's no way in irb
to save what you typed in and run it again later. To do that, we'll need to put our code in a Ruby program file. If you haven't already chosen a code editor, go ahead and
install a code editor before moving on.
Choose A Code Editor
Throughout the course we'll be creating a Ruby program by putting Ruby code in one or more files. You'll need to choose a code editor to create and edit these files. The editor doesn't need to have a ton of features. In fact, a basic code editor that has Ruby syntax highlighting and a file/directory browser works best.
In the videos we use TextMate (Mac only). However, you could just as well use Visual Studio Code which runs equally well on Mac, Windows, or Linux. And it's free!
Create and Run a Ruby Program File
Once you have a good code editor installed, you're ready to write your first Ruby program file.
-
Start by creating a directory called
studio_game
to hold the files you create while taking this course and change into this directory. The commands to do that are the same regardless of which operating system you're using, but the directory structure is slightly different.If you're running Mac OS X or Linux, create the
studio_game
directory in your home directory (represented by the tilde character). To create the directory and change into it, type the following two commands at the command prompt (not in the code editor):mkdir ~/studio_game cd ~/studio_game
If you're running Windows, create the
studio_game
directory in the top-levelC:\
directory (represented by the backslash character) since Windows doesn't really have the concept of a user's home directory. To create the directory and change into it, type the following two commands at the command prompt (not in the code editor):mkdir \studio_game cd \studio_game
-
Next, create a new file called
studio_game.rb
. Make sure to save the file in thestudio_game
directory we created in the previous step. -
We want the game to start by printing a welcome message. So in your
studio_game.rb
file, type in the Ruby code to print "Welcome!" to the screen. Remember thatirb
always prints results to the screen, whereas in a Ruby program file you need to useputs
to print results to the screen. -
Save the file!
-
Now, back over at your operating system command prompt (not in the
irb
session), run your program using theruby
command and passing the name of your Ruby program file as the argument:ruby studio_game.rb
Tip: Most Ruby-aware code editors and IDEs offer a way to run Ruby program files from within the editor. For example, if you're using TextMate, you can run Ruby program files by pressing
Command+R
. However, this isn't necessary. You can always run Ruby files from the command line using theruby
command shown above. -
Now, similar to what we did in the video, change your program to assign the welcome message to a variable named
greeting
, and print it uppercase three times in a loop.Don't worry if the syntax doesn't make sense at this point. We'll learn more about strings, variables, methods, and blocks in future exercises. The important part is that you know how to run a Ruby program file.
-
After printing the greeting, it might be handy for the game to print the time it started. Use the built-in
Time
class to print the current time. -
Run the program to make sure you get what you expect!
TextMate Users: Now would be a good time to configure TextMate to always use the version of Ruby installed by rbenv.
Configure TextMate to Use Ruby 3
By default, TextMate will use the system-installed version of Ruby rather than the version you installed with rbenv. So if you want to be able to run your Ruby program from inside TextMate, you'll need to configure TextMate to use the Ruby version installed by rbenv. Here's how to do that:
-
First, get the rbenv name of the Ruby version you want to use in TextMate. Running the
rbenv versions
command in your Terminal session will print out all the rbenv version names. For example:$ rbenv versions 3.1.2 3.1.3 * 3.1.4
In this example output, the name of the currently selected Ruby version is
3.1.4
which is the version we want to use in TextMate. -
Next, open up TextMate and navigate to the TextMate -> Preferences menu item. If you're using TextMate 2, select the Variables panel. If you're using TextMate 1, select the Advanced panel and the Shell Variables tab.
Then find the
PATH
variable and add$HOME/.rbenv/bin:$HOME/.rbenv/shims:
to the front.Then change or add the
TM_RUBY
variable and set its value to$HOME/.rbenv/shims/ruby
. -
Now quit TextMate and open your
studio_game.rb
program file. TextMate should now be using the proper version of Ruby (theTM_RUBY
setting has taken effect). To verify the version of Ruby being used, add the following line to the top of yourstudio_game.rb
program file:puts RUBY_VERSION
-
Save the file.
-
Run the file by pressing
Command+R
. -
You should see "3.1.4".
-
Go ahead and remove the line we added in step #3.
-
Finally, you might want to download the TextMate Shortcuts PDF file (right-click to save) for reference as you work through the exercises. It's a cheat sheet of the most common TextMate keyboard shortcuts you may see us use in the videos.
Install the mate
Command-Line Utility
In the video we used the mate
command-line utility to create
new Ruby files. You can install mate
by using the Textmate -> Preferences -> Terminal menu item.
Wrap Up
Congratulations, you just wrote your first Ruby program! You also:
- assigned your first variable (
greeting
) - called your first method (
upcase
) - used your first class (
Time
) - wrote your first loop (
3.times
)
You're now ready to add the first player to the game. Onward and upward to the next section!
Daily Plan
The key to learning anything new is consistent, deliberate practice and it can help to have an actual plan to follow. So we designed a simple daily plan to help you lock in a routine and track your progress.
Download the daily plan, print it, and use your favorite-colored Sharpie to cross off the days. Each day you'll add one more facet of Ruby to your repertoire!