The Pragmatic Studio

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.

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

  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 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
    
  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 the string literal "Hello, Ruby!".

  5. Assign that string to a variable named greeting.

  6. Finally, use the upcase method to print the greeting in all upper case letters.

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

  8. When you're done, exit the irb session by typing, wait for it... exit (or Ctrl-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.

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.

  1. 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-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 called 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 "Welcome!" 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, back over at your operating system command prompt (not in the irb session), run your program using the ruby 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 the ruby command shown above.

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

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

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

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!

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.