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:
irb and by creating Ruby program files. Here's where
the fun begins...
1. Run Ruby Interactively
irb session is a great place to start experimenting with Ruby
code. It gives you instant feedback.
Fire up an
irbsession from your command line:
Once it starts, you'll see a prompt where you can type in any Ruby code. Here's what our
irbprompt looks like:
If your prompt looks slightly different than ours, don't worry about it. For example, you may see something like this:
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
Not surprisingly, Ruby is pretty good at math. So a really simple use of
irbis 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
=>, like this
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
Finally, use the
upcasemethod 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
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
irbsession by typing, wait for it...
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
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.
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_gameto 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_gamedirectory 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_gamedirectory 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
Next, create a new file called
studio_game.rb. Make sure to save the file in the
studio_gamedirectory we created in the previous step.
We want the game to start by printing a welcome message. So in your
studio_game.rbfile, type in the Ruby code to print "Welcome!" to the screen. Remember that
irbalways prints results to the screen, whereas in a Ruby program file you need to use
putsto print results to the screen.
Save the file!
Now, back over at your operating system command prompt (not in the
irbsession), run your program using the
rubycommand and passing the name of your Ruby program file as the argument:
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
rubycommand 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
Timeclass 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 versionscommand 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.4which 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
PATHvariable and add
$HOME/.rbenv/bin:$HOME/.rbenv/shims:to the front.
Then change or add the
TM_RUBYvariable and set its value to
Now quit TextMate and open your
studio_game.rbprogram file. TextMate should now be using the proper version of Ruby (the
TM_RUBYsetting has taken effect). To verify the version of Ruby being used, add the following line to the top of your
Save the file.
Run the file by pressing
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.
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.
Congratulations, you just wrote your first Ruby program! You also:
- assigned your first variable (
- called your first method (
- used your first class (
- wrote your first loop (
You're now ready to add the first player to the game. Onward and upward to the next section!
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!