From: Josh Cheek Date: 2009-09-09T16:34:50+09:00 Subject: Re: Tutorial challenge program help --000e0cd2a024cdc9e80473200757 Content-Type: text/plain; charset=ISO-8859-1 On Tue, Sep 8, 2009 at 8:13 PM, Chris Logan wrote: > Hello all im really new to ruby as in a few days and getting into it. i > was following a tutorial found on > http://pine.fm/LearnToProgram/?Chapter=00 in later chapters it made a > challenge to write a program involving a looping a deaf grandma i > started it and have it for the most part working buts its doing a few > things that i dont fully understand im guessing these are bugs O.o? > > reply = ' ' > replytt = ' ' > puts 'Ahh theres my grandson' > while replytt != 'BYE' > while reply != 'BYE' > reply = gets.chomp > if reply == reply.upcase > puts 'No not since 19' + (rand(5)).to_s + (rand(10)).to_s + '!' > reply = gets.chomp > else > puts 'WHAT\'S THAT SONNY SPEAK UP!' > reply = gets.chomp > end > end > if reply == 'BYE' > puts 'Do you really have to go?' > replyt = gets.chomp > reply = replyt > if replyt == 'BYE' > puts 'Don\'t you wanna hear about the time I met your granda?' > replytt = gets.chomp > replyt = replytt > reply = replyt > else > puts 'WHAT\'S THAT SONNY SPEAK UP!' > replyt = replytt > reply = replyt > reply = gets.chomp > end > end > end > puts 'Come back and visit your old granny again soon.' > puts 'Ill tell you the time I...' > puts 'zzzz...' > > a few things first off since i cannot seem to find it on google i dont > know how to make rand pop out anything thats not 0- a number for > instance i wanted it to be like the tutorial said and make it 1930- 1950 > for the year the second is during if reply == reply.upcase it gives me a > blank line i have to hit enter again before it gives the No not since > [year] also during the second BYE if replyt == 'BYE' even if i put an > answer that isnt in caps lock it gives it me a respons as if it was. > thats all i can remeber right now also any tips on just making the whole > thing smoother would be appreciated. I'm quite proud of this as its my > first real project i did. > -- > Posted via http://www.ruby-forum.com/. > > Hi, Chris, I think that extracting pieces of functionality out into methods would make your program a lot simpler. I know it can be frustrating to work for 6 hours on a project like this, so let me show you how I would approach the problem. I put a lot of comments in, because I don't know how much you understand, forgive me if I stress things you are already well aware of. # this method gets the input from the user # 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. # # the last line of a method is always returned, so in this case, # we set the variable @input, and return it's value # this is because the assignment operator, =, is itself a method # which returns the value that was assigned. def get_input print "Say to deaf grandma: " #prompts the user for information @input = gets.chomp #set @input, return it's value end # this function returns a year from 1930 - 1950 def get_year # we use double quotes, then embed the values we want within them # when we do it this way, the .to_s method is automatically called # on whatever the value happens to be. # so this is the same as "19" + (rand(21)+30).to_s "19#{rand(21)+30}" #variability of 21 years, offset by 30 years end # this is a little trickier, here we open up the String class # and we add the method to it called shouted? This will then be # available to any given string. # We can access that string with the self variable # # So if the string is equal to itself in upcase, then it has been # shouted, this value will be returned # try it out with something like # p "did not shout".shouted? # p "DID SHOUT".shouted? # when we put a question mark on the end of a function or method, # it indicates that a boolean value will be returned. A boolean # value is just true or false. class String def shouted? self == self.upcase #returns true if the string is uppercase end end # now that we have made our functions and methods to help us, our # program logic becomes much simpler, the entire program can be # written like this while get_input != "BYE" # remember, our instance variable @input # was set by the get_input method, it is the String that the user # submitted. Since it is a String, and we added the .shouted? # method to the String class, we can call that method on it if @input.shouted? puts "NO, NOT SINCE #{get_year}!" #embed the year in the string else puts "HUH?! SPEAK UP, SONNY!" end puts #a blank line to make it more legible end puts "BYE, SONNY!" #grandma's last response --000e0cd2a024cdc9e80473200757--