Da Sobota 01 Aprl 2006 08:53 JB napsal: > 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!? > <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. > > 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 Vallner