From: Josh Cheek Date: 2009-09-10T07:32:17+09:00 Subject: Re: Tutorial challenge program help --000e0cd6c8548783f604732ca7ca Content-Type: text/plain; charset=ISO-8859-1 On Wed, Sep 9, 2009 at 2:44 PM, Chris Logan wrote: > Josh Cheek wrote: > > On Tue, Sep 8, 2009 at 8:13 PM, Chris Logan > > wrote: > > # it sets it into the instance variable @input > > # we use an instance variable because it will continue > > # to exist outside the method. You know it is an instance > > # variable because it begins with an @ symbol. If it did not, it > > # would be a local variable, and "die" after the method returned. > I do not fully understand this. > Hi, Chris, for right now, I think it is probably best to just accept that you don't understand it, and just treat it like a variable that you have access to inside and outside of functions. When you get to the OOP (Object Oriented Programming) portions of your learning, it will become clear, I think. But for me to explain it would require some OOP theory. But when you get there, you will learn about scope, different types of variables, the differences between functions and methods (granted that I usually just call everything a method), and so on. It should then be clear to you why a variable called @something works differently than a variable called something. You'll also learn why you can call "my string".shouted? rather than doing something like shouted?( "my_string" ) Until then, you learn about instance variables and OOP, it would probably be best to just chant the Apple mantra of "it just works". An alternative way to do this would be like this: def get_input print "Say to deaf grandma: " #prompts the user for information gets.chomp #return the input end Then down where we use it, instead of saying while get_input != "BYE" if @input.shouted? puts "NO, NOT SINCE #{get_year}!" else puts "HUH?! SPEAK UP, SONNY!" end puts end we could say: while (input = get_input) != "BYE" if input.shouted? puts "NO, NOT SINCE #{get_year}!" else puts "HUH?! SPEAK UP, SONNY!" end puts end The difference here is that it returns the input, then we assign it to a local variable with the (input = get_input) and then compare that input to the string "BYE" This is what I would probably do in reality, but I thought the (input = get_input) != "BYE" would be more confusing. The reason I do it that way is so that the input is set where it is compared, that saves me from a lot of edge cases like the first time through the loop not having the value of input be initialized, or other esoteric ordering of when to get input verses when to respond to it. You'll notice it was in trying to handle this logic that you ended up prompting two times, causing the bug you noted. So that is why I wanted the assignment to take place right where the comparison is made. >>puts "NO, NOT SINCE #{get_year}!" #embed the year in the string else >Do I use the # symbol? I read that that denotes a comment and anything >after it would be ignored by Ruby. To embed something in a string, you have the opening and closing " symbols, and inside them you then put #{ ... } where whatever is in there gets interpreted and placed into the string. The # symbol does indicate a comment, but not when it is inside of a string. Note that you need to use double quotation " to do this, single quotation ' will not embed strings. >* Except for very small snippets, code should probably not go in the post. >There are sites (such as pastie.org) which are perfect for this purpose >and will even syntax highlight your code. I've had people here tell me not to use external sites (I posted some of my code on pastie) as it does not preserve the code if the site goes down. --000e0cd6c8548783f604732ca7ca--