The Pragmatic Studio

Running Ruby



Now that we have Ruby installed, it's time to start writing 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 because it gives you instant feedback!

  1. Fire up an IRB session from your command line by using the irb command that was installed as part of Ruby:


    Once it starts, you'll see a prompt where you can type in any Ruby code. Here's what our prompt looks like:


    If your prompt looks slightly different than ours, don't worry about it. For example, you may see something like this:


    However your prompt looks, this is where you type in Ruby expressions for irb to evaluate.

  2. 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 divides two numbers, then press Return:

    >> 12 / 4

    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
  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.

  4. Next, create a string literal with your favorite phrase or saying.

  5. Then multiply that string by a number and assign the result to a variable.

    Tip: Remember, you can use the up and down arrow keys on your keyboard to navigate through the IRB session history.

  6. Finally, exit out of the IRB session:

    >> exit

    Another way to end an IRB session is to type the end-of-file character for your operating system. For example, on a Mac it's CTRL+D.

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, prints the result, and loops back for more input. And that's super handy! 👌

2. Write a Ruby Program File

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 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.

Once you have a code editor installed, you're ready to write your first Ruby program file.

  1. Start by creating a directory named studio_game to hold the files you create while taking this course and change into that 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-level C:\ 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
  2. Next, create a new file named studio_game.rb. Make sure to save the file in the studio_game directory we created in the previous step.

  3. 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 "Let's play a game!" to the screen.

    Remember that irb always prints results to the screen, whereas in a Ruby program file you need to use puts to print results to the screen.

  4. Save the file!

  5. Now, at your operating system command prompt (not in the irb session) in the studio_game directory, run your program using the ruby command and give it the name of your Ruby program file as the argument:

    ruby studio_game.rb

    Does it print your welcome message?

  6. After printing the welcome message, the game should print a separating line with repeating emojis, such as:

    Let's play a game!

    Print your favorite emoji 25 times—without actually typing the emoji that many times! There are at least 3 ways to do this. Try to come up with as many as you can.

  7. Run the program to make sure you get what you expect!

We'll learn more about strings and variables in future exercises. The takeaway of this exercise is that you know how to run a Ruby program file.


The full solution for this exercise is in the running-ruby directory of the code bundle.

Wrap Up

Congratulations, you just wrote your first Ruby program! 🏆

You're now ready to add the first player to the game. Onward and upward to the next section!

All course material, including videos and source code, is copyrighted and licensed for individual use only. You may make copies for your own personal use (e.g. on your laptop, on your iPad, on your backup drive). However, you may not transfer ownership or share the material with other people. We make no guarantees that the source code is fit for any purpose. Course material may not be used to create training material, courses, books, and the like. Please support us by encouraging others to purchase their own copies. Thank you!

Copyright © 2005–2024, The Pragmatic Studio. All Rights Reserved.