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.

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”].

TESTHEAD’s TAKEAWAYS:

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).