# Question about 'how it looks at things'

**URL:** <https://www.ruby-forum.com/t/question-about-how-it-looks-at-things/56575>\
**Category:** Ruby\
**Created:** [April 1, 2006, 8:56am UTC](https://www.ruby-forum.com/t/question-about-how-it-looks-at-things/56575 "2006-04-01T08:56:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 1, 2006, 8:56am UTC](https://www.ruby-forum.com/t/question-about-how-it-looks-at-things/56575/1 "2006-04-01T08:56:37Z")

</div>

Hi gang,

Okay, this is referring to the ‘Learn to Program’ thread a little bit.

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

puts ‘(Say Hi to grandma)’  
say = gets.chomp  
while say != say.upcase  
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 + ‘!’  
end  
end

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!?

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.

Thanks,

JB

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 1, 2006, 9:30am UTC](https://www.ruby-forum.com/t/question-about-how-it-looks-at-things/56575/2 "2006-04-01T09:30:03Z")

</div>

DÅ?a Sobota 01 AprÃ­l 2006 08:53 JB napÃ­sal:

> puts ‘(Say Hi to grandma)’  
> Does ruby look at things and work its way down from the beginning or does  
> if say == say.upcase  
> Hi gramma  
> little easier…then again, the answer I get might just confuse me more,  
> lol.
> 
> Thanks,
> 
> JB

Well, unless you did Horrible Things to your Ruby interpreter, it should  
execute statements sequentially, in the order as they occur in the file.

However, your indentation is horribly confusing, and I have a hunch that  
your  
problem is related - you lost track of what’s happening in the while  
loop.

You do know what a loop is, don’t you? Just in case this happens to be  
the  
problem, it’s -supposed- to “go back up” and repeat the loop as long as  
you  
don’t say “BYE” to Grandma…

David V.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 1, 2006, 3:29pm UTC](https://www.ruby-forum.com/t/question-about-how-it-looks-at-things/56575/3 "2006-04-01T15:29:44Z")

</div>

JB asked:

> making sense what I’d like to know? The reason I ask is, if I try the  
> date = rand(21) + 1930  
> HUH!? SPEAK UP! WHAT’RE YOU WHISPERIN’ FOR!?  
> 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
