Ruby the Hard Way: Exercise 7 – 10


More Printing

puts “Mary had a little lamb.”
puts “It’s fleece was white as #{‘snow’}”   #snow is no variable, it prints the literal string 
puts “And everywhere that Mary went.”
puts “.” * 10 # what’d that do   #prints out “.” 10 times


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


# watch that print vs. puts on this line what’s it do?

print end1 + end2 + end3 + end4 + end5 + end6      #both words are on the same line
puts end7 + end8 + end9 + end10 + end11 + end12




Printing, Printing …


formatter = “%{first} %{second} %{third} %{fourth}”

puts formatter % {first: 1, second: 2, third: 3, fourth: 4}    #different way to print out 1-4
puts formatter % {first: “one”, second: “two”, third: “three”, fourth: “four”}
puts formatter % {first: true, second: false, third: true, fourth: false}
puts formatter % {first: formatter, second: formatter, third: formatter, fourth: formatter}


#prints out the whole string which is defined for formatter 4 times 

puts formatter % {

first: “I had this thing.”, 

second: “That you could type up right”,

third: “But it didnt sing”,

fourth: “So i said goodnight”


#same way to print out strings in only one line 

%{} does the same as %{}


Printing, Printing, Printing …


days = “Mon Tue Wed Thu Fri Sat Sun”
months = “Jan\nFeb\nMar\nApr\nMay\nJun\nJul\nAug”  #\n is skipping one line like ENTER

puts “Here are the days: #{days}”

puts “Here are the months: #{months}”


puts %q{
There’s something going on here.
With the three double-quotes.
We’ll be able to type as much as we like.
Even 4 lines if we want, or 5, or 6.

#q does center whats in {}



Escape Sequenzes


tabby_cat = “\tI’m tabbed in.”   #TABBED in 
persian_cat = “I’m split\non a line” #breaks line like ENTER
backslash_cat = “I’m \\ a \\ cat.”   #to be able to print out “\” 

fat_cat = “””

I’ll do a list:

\t* Cat food

\t* Fishies

\t* Catnip\n\t* Grass


puts tabby_cat

puts persian_cat

puts backslash_cat

puts fat_cat


Escape Sequenzes: Come in handy if you want to write doublequotes (“”) in a string. If you would write: puts “hello, I’m “big” ” , the program would think the string ends right before the word “big”.

>> puts “hello, I’m \”big\”


Escape What it does.
\\ Backslash ()
\' Single-quote (‘)
\" Double-quote (“)
\a ASCII bell (BEL)
\b ASCII backspace (BS)
\f ASCII formfeed (FF)
\n ASCII linefeed (LF)
\r ASCII Carriage Return (CR)
\t ASCII Horizontal Tab (TAB)
\uxxxx Character with 16-bit hex value xxxx (Unicode only)
\v ASCII vertical tab (VT)
\ooo Character with octal value ooo
\xhh Character with hex value hh
 fig2: Escape Sequences, “Ruby the Hard way”

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s