ABCs and 123s
Exercises
Objective
Now that you know a bit more about numbers and strings, let's format the game's welcome message and add a player! 🧍🏻♂️
1. Print a Countdown
Double-quoted strings support various escape sequences that start with a backslash character. For example, you use \n
for a newline character and \t
for a tab character. When a double-quoted string is evaluated, Ruby looks for these escape sequences and replaces them with their equivalent character.
Let's use some escape sequences to format the game's welcome message...
-
In your
studio_game.rb
file, use a double-quoted string to print the game's welcome message formatted like this:Let's play a game! 3 2 1
Notice that each number is printed on a separate line and indented with a tab. And put a blank line between the last number and the emojis you printed in the previous exercise.
-
Make sure to run the program file to check the formatting!
-
Now change the double-quoted string to a single-quoted string, and what do you expect to happen when you run the program?
-
Finally, change it back to a double-quoted string to get all the formatting goodness. 😎
2. Print a Player's Info
A player in the game will have a name (a string) and an initial health value (an integer number). And we need a way to reference a player's name and health from potentially multiple places in our program. To do that, we'll need to assign the name and health values to variables.
Variables simply name things in our program. It's important to give the variables good names so it's easy to tell what they're referencing. Ruby has some variable naming rules: variable names can be a mix of letter and numbers (or an underscore), and the first character of a variable name must be a lowercase letter or an underscore.
For example, a player's name could be stored in the variable
name
. Sometimes the best variable name has more than one word, in which case you separate the words with an underscore. For example, a good name for a player's high score would be high_score
.
With that in mind, let's create our first player...
-
The name of our first player is "Finn". Create a variable to remember that name. In other words, assign the player's name to the variable.
-
Finn's initial health value is 60. Create a second variable and assign the health value to it.
-
Using those two variables, create the double-quoted string "Finn's health is 60" and print it out.
In addition to supporting various escape sequences, double-quoted strings also support interpolation. You want to interpolate the values of the two variables into the string.
-
Run the program to check your work!
-
Now that you're getting the hang of it, give this a try: How would you change the double-quoted string to triple Finn's health?
-
OK, now what if you wanted to divide Finn's health by 9 and have the answer printed out with decimals (a float) or without decimals (an integer)?
-
Experimenting with code is a great way to learn, so play around a bit until you're comfortable with numbers and strings.
Solution
All the exercise solutions for the game program you're writing, as well as the example code for the movie program we're writing in the videos, is available for download in the code bundle file (right-click to save). No peeking until you try! 🫣
When you unzip the file, you'll end up with a directory named pragstudio-ruby-2ed-code
. Inside that directory, you'll find two sub-directories: exercise-solutions
and video-code
. The code in each of those directories is organized into directories matching the course modules.
The full solution for this exercise is in the
numbers-strings
directory of the
code bundle.
Wrap Up
Double-quoted strings have a lot of built-in smarts, and there's no penalty for using them instead of single-quoted strings. So from now on, we'll use double-quoted strings.
Now the game doesn't do much yet, but it's a good start. In the next section we'll spiff up the game's intro by creating objects and calling their methods!
Ruby in Rails
Rails is a web application framework written in Ruby. How is Ruby used in the context of a Rails application? At end of each exercise, we'll include a section called "Ruby in Rails" that will explain how the Ruby concepts you learned in the exercise are used in Rails.
And of course, numbers and strings are used all over the place in Rails apps! Here are just a few quick examples...
-
A record in the database can have fields that are strings and integers:
Movie.create(title: "Avengers", total_gross: 1223641414)
-
In the router you use strings to match incoming HTTP requests and run a controller's action:
get "signup" => "users#new"
-
After a successful login, you might redirect and display a notice message that includes the user's name:
redirect_to account_url, notice: "Welcome back, #{user.name}!"