Day 16: I Continue to Learn Ruby the Hard Way

So, I am continuing on. I am going to speed through these today as I go over stuff that I learned in the Ruby on Rails tutorial. Please keep up!

I think, after a few days of this, that Zed Shaw’s approach is to throw stuff at you and have you learn by just coding. CODE. CODE HARDER.

Exercise 7: More Printing

PS C:\ruby> ruby ex7.rb
Mary had a little lamb.
Its fleece was white as snow.
And everywhere that Mary went.
……….
CheeseBurger

Exercise 8: Printing, Printing

I typed:

and it outputted:

PS C:\ruby> ruby ex8.rb
1 2 3 4
one two three four
true false true false
%{first} %{second} %{third} %{fourth} %{first} %{second} %{third} %{fourth} %{first} %{second} %{third} %{fourth} %{firs
t} %{second} %{third} %{fourth}
I had this thing. That you could type up right. But it didn’t sing. So I said goodnight.

Exercise 9: Printing, Printing, Printing

Here is what I typed:

PS C:\ruby> 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 three double-quotes.
We’ll be able to type as much as we like.
Even 4 lines if we want, or 5, or 6.

Exercise 10: What Was That?

So, a few new things.

\n puts a new line….

Also, escape sequences are useful. So, if you want to put quotes in a string you have to escape them, like so:

and we get:

PS C:\ruby> ruby ex10.rb
I “love” you.

This is important because Ruby may think that second quote is the end of the string. here is what happens if I remove the backslash.

PS C:\ruby> ruby ex10.rb
ex10.rb:1: syntax error, unexpected tIDENTIFIER, expecting end-of-input
puts “I “love” you.”

So, on with lesson 10.

PS C:\ruby> ruby ex10.rb
I’m tabbed in.
I’m split
on a line.
I’m \ a \ cat.

I’ll do a list:
* Cat food
* Fishies
* Catnip
* Grass

And here are some escape sequences and what they do. You can find this table here, also.

EscapeWhat it does
\\Backslash ()
\'Single-quote (')
\"Double-quote (")
\aASCII bell (BEL)
\bASCII backspace (BS)
\fASCII formfeed (FF)
\nASCII linefeed (LF)
\r ASCIICarriage Return (CR)
\t ASCIIHorizontal Tab (TAB)
\uxxxxCharacter with 16-bit hex value xxxx (Unicode only)
\vASCII vertical tab (VT)
\oooCharacter with octal value ooo
\xhhCharacter with hex value hh

Study drills!

1. Memorize all the escape sequences by putting them on flash cards.

2. Use ”’ (triple-single-quote) instead. Can you see why you might use that instead of “””?

PS C:\ruby> ruby ex10.rb
I’m tabbed in.
I’m split
on a line.
I’m \ a \ cat.

I’ll do a list:
\t* Cat food
\t* Fishies
\t* Catnip\n\t* Grass

So, it appears using three single-quotes prints everything inside them exactly, regardless of the code inside it…

3. Combine escape sequences and format strings to create a more complex format.

OK, I tried throwing these around in here. It got a little weird…

PS C:\ruby> ruby ex10.rb
I’m tabbed in.
I’m split
on a line.
I’m \ a \ cat.

I’ll do a list:
* Cat food
* Fishies
* Catnip
* Grass

I have eaten
the plums
that were in
the icebox

♂and which
you were probably

saving
♀for breakfast

oooForgive me
they were delicious
so sweet
and so cold

Hat tip to William Carlos Williams.

Exercise 11: Asking Questions

I typed:

and the output is:

PS C:\ruby> ruby ex11.rb
How old are you? 92
How tall are you? 8’7″
How much do you weigh? 25
So, you’re 92 old, 8’7″ tall, and 25 heavy.

The computer asked me for my input for each of the questions.

OK, Study drills!

1. Go online and find out what Ruby’s gets.chomp does.

Well, I bought Chris Pine’s book and think this is a great opportunity to try it out. It looks like the .chomp method gets rid of any Enter characters at the end of a string. An example:

And the result:

PS C:\ruby> ruby ex11.rb
How old are you? 36
How tall are you? 6′
How much do you weigh? 400
So, you’re 36
old, 6′
tall, and 400
heavy.

2. Can you find other ways to use it? Try some of the samples you find.

MAD LIBS! Here is what I wrote:

and here is my go at it:

PS C:\ruby> ruby studydrill11b.rb
Give me a color. Red
Give me a superlative ending in “est”. stupidest
Give me an adjective. bloated
Give me a plural body part. ears
Give me a singular body part.tongue
Give me a noun. bus
Give me a plural animal. aardvarks
Give me an adjective. gross
Give me an adjective. tiny
Give me one more adjective. hollow

OK, here is your story!

The Red Dragon is the stupidest Dragon of all.

It has bloated ears, and a tongue
shaped like a bus. It loves to eat aardvarks, although
it will feast on nearly anything. It is gross and tiny.
You must be hollow around it, or you may end up its meal!

3. Write another “form” like this to ask some other questions.

print “What is your name?”
name = gets.chomp
print “What is your quest?”
quest = gets.chomp
print “What’s the air speed velocity of an unladen swallow?”
swallow = gets.chomp

puts “So, your name is #{name}, you are on a quest for #{quest} and you think the air
speed velocity of an unladen swallow is #{swallow}.”

PS C:\ruby> ruby studydrill11.rb
What is your name?Nick
What is your quest?to learn ruby
What’s the air speed velocity of an unladen swallow?um, 25
So, your name is Nick, you are on a quest for to learn ruby and you think the air speed velocity of an unladen swallow is um, 25.

OK, that is enough for tonight! I had a lot of fun making the Mad Lib. I will be starting on Exercise 12: Prompting People for Numbers tomorrow.