Running Ruby
Exercises
Objective
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!
-
Fire up an IRB session from your command line by using the
irb
command that was installed as part of Ruby:irb
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:
irb(main):001:0>
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 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
-
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 a string literal with your favorite phrase or saying.
-
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.
-
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.
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 Visual Studio Code which runs equally well on Mac, Windows, or Linux. And it's free! We also use the following VS Code extensions:
-
Code Runner extension for running Ruby program files, with the following VS Code settings:
"code-runner.clearPreviousOutput": true, "code-runner.showExecutionMessage": false, "code-runner.ignoreSelection": true,
You can add these settings by opening your user settings in VS Code using the Preferences: Open Settings (JSON) command.
-
Material Theme extension for the VS Code color theme.
Once you have a code editor installed, you're ready to write your first Ruby program file.
-
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-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 named
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 "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 useputs
to print results to the screen. -
Save the file!
-
Now, at your operating system command prompt (not in the
irb
session) in thestudio_game
directory, run your program using theruby
command and give it the name of your Ruby program file as the argument:ruby studio_game.rb
Does it print your welcome message?
-
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.
-
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.
Solution
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!