Created
May 24, 2011 19:23
-
-
Save dgonzo/989459 to your computer and use it in GitHub Desktop.
Method call from ternary
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@byes = 0 | |
def prompt(*args) | |
print(*args) | |
gets.chomp | |
end | |
def random_year | |
rand(21) + 1930 | |
end | |
def introduce_grandma | |
"Grandma is listening.\nGreet her, ask her a question.\nJust make sure you shout; she is hard of hearing." | |
end | |
def you_say_to_grandma | |
prompt " You say: " | |
end | |
def grandma_responds_to (something = you_say_to_grandma) | |
unless something != "BYE" then | |
@byes > 2 ? grandma_says_farewell : @byes += 1 | |
else | |
something == something.upcase! ? "Grandma says: HUH? SPEAK UP, SONNY!" : "Grandma says: NO, NOT SINCE #{random_year}" | |
end | |
end | |
def grandma_says_farewell | |
print "SO LONG SONNY! NICE TALKING TO YOU!" | |
end | |
puts conversation = introduce_grandma | |
print "#{conversation}\n" while conversation = grandma_responds_to |
There are a few ways to solve this. The biggest thing is that you don't reset the counter to 0 to enforce the consecutive BYE rule. You start at 0, and check first then increment and so you will exit when that count gets to 3 (larger than 2), which means 4 iterations of it. Not what you want. So, two points:
Reset a counter (You chose to use @bye) to 0 if granny doesn't hear BYE, and increment properly.
I placed the code in our Dropbox.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Just loaded the whole thing in, minus the calls to the methods into irb and say @byes incrementing. Then I killed the process in irb and checked @byes and the ternary logic on line 22: