Up to now we have just been typing “puts” and then following on with some text in quotes, or variable names not in quotes. The values of the variables get printed to the screen. The text between the quotes is referred to as a “string”. Yeah, I know, this is really basic stuff so far, and that’s fine. The intermediate hill is coming up soon, though so there is some value to paying attention in these earlier phases :).
This particular exercise shows how to do more than just print a set of characters between double quotes. Variables can also be inserted into the text strings so that the lines printed out aren’t just static text.
So here’s the first exercise:
As usual, just type this in even if you do not understand it and make it exactly the same.
1 my_name = ‘Zed A. Shaw’
2 my_age = 35 # not a lie
3 my_height = 74 # inches
4 my_weight = 180 # lbs
5 my_eyes = ‘Blue’
6 my_teeth = ‘White’
7 my_hair = ‘Brown’
9 puts “Let’s talk about %s.” % my_name
10 puts “He’s %d inches tall.” % my_height
11 puts “He’s %d pounds heavy.” % my_weight
12 puts “Actually that’s not too heavy.”
13 puts “He’s got %s eyes and %s hair.” % [my_eyes, my_hair]
14 puts “His teeth are usually %s depending on the coffee.” % my_teeth
16 # this line is tricky, try to get it exactly right
17 puts “If I add %d, %d, and %d I get %d.” % [
18 my_age, my_height, my_weight, my_age + my_height + my_weight]
[…and here we go]
What You Should See
$ ruby ex5.rb
Let’s talk about Zed A. Shaw.
He’s 74 inches tall.
He’s 180 pounds heavy.
Actually that’s not too heavy.
He’s got Blue eyes and Brown hair.
His teeth are usually White depending on the coffee.
If I add 35, 74, and 180 I get 289.
[yep 🙂 ]
Change all the variables so there isn’t the my_ in front. Make sure you change the name everywhere, not just where you used = to set them.
[Done, Zed’s point being, the variables once defined will look for those values specifically. If the ‘my_’ isn’t deleted everywhere, you will have a variable with no assignment or definition, and you will get an error. Fun, huh :)?]
Try more format sequences.
[I added a couple to the code and made some separate puts statements to specify what they are]
Search online for all the Ruby format sequences.
Found these at ruby-doc.org. Not sure if it’s all of them, but it’s a pretty good list:
b – Convert argument as a binary number.
c – Argument is the numeric code for a single character.
d – Convert argument as a decimal number.
E – Equivalent to `e’, but uses an uppercase E to indicate the exponent.
e – Convert floating point argument into exponential notation with one digit before the decimal point. The precision determines the number of fractional digits (defaulting to six).
f – Convert floating point argument as [-]ddd.ddd, where the precision determines the number of digits after the decimal point.
G – Equivalent to `g’, but use an uppercase `E’ in exponent form.
g – Convert a floating point number using exponential form if the exponent is less than -4 or greater than or equal to the precision, or in d.dddd form otherwise.
i – Identical to `d’.
o – Convert argument as an octal number.
s – Argument is a string to be substituted. If the format sequence contains a precision, at most that many characters will be copied.
u – Treat argument as an unsigned decimal number.
X – Convert argument as a hexadecimal number using uppercase letters.
x – Convert argument as a hexadecimal number.
Try to write some variables that convert the inches and pounds to centimeters and kilos. Do not just type in the measurements. Work out the math in Ruby.
[See below. I used the % [height * 2.54] evaluation to convert height in inches into a temporary conversion value. Note that height has not actually been given a new value. Also, % [weight / 2.2] did the same thing; it made a temporary conversion value of weight and represents it in kilograms (with 2 points of decimal precision). Weight is still the same value, if we called weight by itself, it would also appear with its original value. This is a good way to do some temporary conversions and not have to litter the code with a bunch of variable declarations. If you wanted to store that intermediate value to be used somewhere else later on in the program, then yes, it would make sense to actually create another variable and then use it there (such as “kilo_weight = weight / 2.2”].
OK, so there are a couple of cool things to consider here. The first is that Zed is already starting to show that we need not be spoon fed in everything. He didn’t give us the character strings to use, he had us go look for them, a valuable skill in our “LMGT” (Let Me Google That) for you world. If they are printed, we’ll just scan them and use something. If we have to look for them, we have to actually think about what we are after and what they might be represented as. Searching for “string modifiers for Ruby” got me less than satisfactory results, but as I did some poking, I saw reference to “sprintf” and thought, OK, how about searching for “sprintf string modifiers”… and ta-daa, the table above is what I got in the first link :).
The second thing is that we can do a lot of things on the fly, especially if the value we need is only going to be used once. This lets us see what the operations are that are needed to create this temporary value that we display. If we’re doing a lot of manipulations, we may want to keep that intermediate value and any others that appear in separate variables. However, a lot of formulas deal with known values and constants, so instead of storing everything independently, we can manipulate a lot of the values on the fly (this should be no big revelation to anyone that has ever worked with a shell script, but hey, I still think it’s pretty cool to be able to do that).