The Pragmatic Studio
get this course sign in

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 text editor, go ahead and install a text editor before moving on.

Create and Run a Ruby Program File

Once you have a good text 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 text 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 text 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 text 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!

Dive Deeper

Want to dive a little deeper? At the end of each exercise, we'll recommend some resources for you to optionally explore on your own if you want to go a bit deeper into a specific topic.

Specifically, you may find it helpful to work through the course with a copy of Programming Ruby (affectionately known as the Pickaxe) by your side. It's both a tutorial and a complete reference to the Ruby language, so it's a great supplement to this course. And to help you get the best of both worlds, we'll reference specific sections in the book for bonus reading.

So here's your first opportunity to venture out on your own. You may want to customize your irb shell or learn commands to control it. To do that, read through Chapter 18 in Programming Ruby.

At some point, you may also want to try some of the other text editors out there. Here are a few commercial editors worth trying:

  • TextMate is a great editor for the Mac and what we use in the videos. (30-day free trial)

  • Sublime Text is a cross-platform text editor that's also very popular with Ruby programmers. (Free trial)

  • RubyMine is a full-blown Ruby and Rails IDE that works on Windows, Mac OS X, and Linux. Many of our alumni highly recommend it once you've learned Ruby. (30-day free trial)

All course material, including videos, slides, 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–2021, The Pragmatic Studio. All Rights Reserved.