Dave Burt
4/1/2006 1:29:00 PM
JB asked:
> I think I need to know 'how' Ruby looks at a script and how it does
> it...at
> least I think that's what I want to know.
> In other words, take a simple script like the one below (I know this one
> 'stops' after 'shouting' an answer, heh):
> ...
> Does ruby look at things and work its way down from the beginning or does
> it
> do one thing and then go back and look at something else and then do it?
> Am I
> making sense what I'd like to know? The reason I ask is, if I try the
> script
> below, I keep getting both 'answers' from 'grandma'.:
>
>
> puts '(Say Hi to grandma)'
> say = gets.chomp
> while say != 'BYE'
> puts 'HUH!? SPEAK UP! WHAT\'RE YOU WHISPERIN\' FOR!?'
> say = gets.chomp
> if say == say.upcase
> date = rand(21) + 1930
> puts 'NO, NOT SINCE ' + date.to_s + '!'
> if say == 'BYE'
> puts 'BYE, BYE!'
> end
> end
> end
>
> **Here's the output:
>
> Hi gramma
> HUH!? SPEAK UP! WHAT'RE YOU WHISPERIN' FOR!?
> HI GRAMMA!
> NO, NOT SINCE 1949!
> HUH!? SPEAK UP! WHAT'RE YOU WHISPERIN' FOR!?
> <waiting for input>
>
> It looks like my script is going back 'up' and then coming back down all
> over again. Does that make sense and does anyone understand what I'm
> asking
> now about 'how' Ruby 'reads' a script or whatever? If I can wrap my mind
> around the 'how' it works, I might be able to figure out these exercises a
> little easier...then again, the answer I get might just confuse me more,
> lol.
I'm not sure I understand the question. It might help you to read the
chapter again, and to type all the code in the chapter into irb as you go,
to get a feel for it.
Here are some notes on how this program goes, after properly indenting it.
puts '(Say Hi to grandma)' #1
say = gets.chomp #2
while say != 'BYE' #3 - if say != 'BYE', continue to #4, otherwise jump to
#13
puts 'HUH!? SPEAK UP! WHAT\'RE YOU WHISPERIN\' FOR!?' #4
say = gets.chomp #5
if say == say.upcase #6 - if say == say.upcase, continue to #7,
otherwise go to #12
date = rand(21) + 1930 #7
puts 'NO, NOT SINCE ' + date.to_s + '!' #8
if say == 'BYE' #9 - if say == 'BYE', continue to #10, otherwise
jump to #11
puts 'BYE, BYE!' #10
end #10
end #11
end #12 - continue from #3
#13
In particular, the trick you need to learn in this chapter is about the loop
and the conditional.
The conditional "if" is just a way to either run some code or not, depending
on the condition; program flow continues after the if block.
On the other hand, loops like "while" provide a way to jump back up, and
repeat from a line that has already been executed. Like an "if", the code
inside it will only be run if the condition is true, but after it's been
through once, the condition is evaluated again, and the block will be run
again if it's still true. Only when the condition is false will the program
flow continue beyond the loop.
Try to use these concepts and the comments I put on your code above to walk
through the program line-by-line and see how the computer gave the output
you listed.
Hope this helps,
Dave