Blog

Exercise 7, 8, 9: Lots More Printing: Learn Ruby The Hard Way: Practicum (TESTHEAD)

On November 6, 2011, in Syndicated, by Association for Software Testing
0

So these are meant to be “chops building” exercises, and mostly just dealing with print statements, so there’s not a lot of excitement going on here. I decided to group these three together since they are effectively the same thing, but I’m doing them :).













Exercise 7

So this is demonstrating the difference between using puts and print.

puts “Mary had a little lamb.”
puts “Its fleece was white as %s.” % ‘snow’
puts “And everywhere that Mary went.”
puts “.” * 10  # what’d that do?

end1 = “C”
end2 = “h”
end3 = “e”
end4 = “e”
end5 = “s”
end6 = “e”
end7 = “B”
end8 = “u”
end9 = “r”
end10 = “g”
end11 = “e”
end12 = “r”

# notice how we are using print instead of puts here. change it to puts
# and see what happens.
print end1 + end2 + end3 + end4 + end5 + end6
print end7 + end8 + end9 + end10 + end11 + end12

# this just is polite use of the terminal, try removing it
puts

[and here we go]

What You Should See

$ ruby ex7.rb
Mary had a little lamb.
Its fleece was white as snow.
And everywhere that Mary went.
……….
CheeseBurger
$

[and that is indeed what I do see]

Extra Credit

For these next few exercises, you will have the exact same extra credit.

Go back through and write a comment on what each line does.

[see below]

Read each one backwards or out loud to find your errors.

From now on, when you make mistakes write down on a piece of paper what kind of mistake you made.

[just some typos and missing quotations with letters]

When you go to the next exercise, look at the last mistakes you made and try not to make them in this new one.

Exercise 8: Printing, Printing

So here’s another printing example, to help us get more in the mood for formatting text. this shows us how we can declare a set of strings and print them following a single variable. Interesting.

formatter = “%s %s %s %s”

puts formatter % [1, 2, 3, 4]
puts formatter % [“one”, “two”, “three”, “four”]
puts formatter % [true, false, false, true]
puts formatter % [formatter, formatter, formatter, formatter]
puts formatter % [
    “I had this thing.”,
    “That you could type up right.”,
    “But it didn’t sing.”,
    “So I said goodnight.”
]

[and here we go]

What You Should See
$ ruby ex8.rb
1 2 3 4
one two three four
true false false true
%s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s
I had this thing. That you could type up right. But it didn’t sing. So I said goodnight.
$

[and yes, we see this]

Extra Credit

Do your checks of your work, write down your mistakes, try not to make them on the next exercise.

Exercise 9: Printing, Printing, Printing

And this is the last of the printing examples, and we do something unique with this one (actually, this reminds me of the <

# Here’s some new strange stuff, remember type it exactly.

days = “Mon Tue Wed Thu Fri Sat Sun”
months = “Jan\nFeb\nMar\nApr\nMay\nJun\nJul\nAug”

puts “Here are the days: “, days
puts “Here are the months: “, months

puts << p=””><>
There’s something going on here.
With the PARAGRAPH thing
We’ll be able to type as much as we like.
Even 4 lines if we want, or 5, or 6.
PARAGRAPH

[and one more time]

What You Should See

$ ruby ex9.rb
Here are the days:
Mon Tue Wed Thu Fri Sat Sun
Here are the months:
Jan
Feb
Mar
Apr
May
Jun
Jul
Aug
There’s something going on here.
With the PARAGRAPH thing
We’ll be able to type as much as we like.
Even 4 lines if we want, or 5, or 6.

[and indeed I do 🙂]

Extra Credit

Do your checks of your work, write down your mistakes, try not to make them on the next exercise.

TESTHEAD’s TAKEAWAYS


So the cool thing is that there are lots of things we can do to print out text and format the text, ranging from individual variables, multiple variables and even using the \<\< notation to create full blocks of text. Nothing earth shattering, but then doing reps in the gym isn’t Earth-shattering either. It does become noticeable later on, though :).

 

Comments are closed.


Looking for something?

Use the form below to search the site:


Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!