Deaf Grandma

Matthew K. wrote in post #1097694:

If it was me, I’d probably replace the ‘while’ statement with ‘loop do’,
and after Granny says goodbye (inside the if-end block) I’d add a
‘break’. I think that just makes it a bit more clear that this is
actually an infinite loop that can be escaped only when certain criteria
are met. Doesn’t really make a difference, though, I don’t think.

Sorry, I forgot to mention that you have a slight duplication of logic,
because you’ve written ‘while count < 3’ and ‘if count == 3’. This
means if Granny turns on her hearing aid you have to remember to change
the 3 in two places in code. (Look up the DRY principle.)

To get around this you should consolidate that logic, and I can think of
two simple ways:

  1. what I said above, with a ‘loop{}’ and ‘break’ structure, or
  2. remove the final ‘if-end’ block and move the puts statement to the
    bottom of the code, after the while loop:

count=0
while count < 3
response=gets.chomp
if ‘BYE’ == response
count+=1
elsif response == response.upcase
puts "NO, NOT SINCE “#{rand(21)+1930}”
count=0
else
puts “HUH?! SPEAK UP SONNY!”
count=0
end
end
puts “OK. Fine. Just leave then.”

Even more like @steve_k61’s code, huh?

Matthew K. wrote in post #1097695:

Matthew K. wrote in post #1097694:

If it was me, I’d probably replace the ‘while’ statement with ‘loop do’,
and after Granny says goodbye (inside the if-end block) I’d add a
‘break’. I think that just makes it a bit more clear that this is
actually an infinite loop that can be escaped only when certain criteria
are met. Doesn’t really make a difference, though, I don’t think.

Sorry, I forgot to mention that you have a slight duplication of logic,
because you’ve written ‘while count < 3’ and ‘if count == 3’. This
means if Granny turns on her hearing aid you have to remember to change
the 3 in two places in code. (Look up the DRY principle.)

To get around this you should consolidate that logic, and I can think of
two simple ways:

  1. what I said above, with a ‘loop{}’ and ‘break’ structure, or
  2. remove the final ‘if-end’ block and move the puts statement to the
    bottom of the code, after the while loop:

count=0
while count < 3
response=gets.chomp
if ‘BYE’ == response
count+=1
elsif response == response.upcase
puts "NO, NOT SINCE “#{rand(21)+1930}”
count=0
else
puts “HUH?! SPEAK UP SONNY!”
count=0
end
end
puts “OK. Fine. Just leave then.”

Even more like @steve_k61’s code, huh?

It really is…Thanks for the pointers. I haven’t learned loop do yet,
so looking forward to that one. I guess the final conditional statement
was really unnecessary since the while loop would keep going until
count==3. Thanks again.

On Mon, Feb 18, 2013 at 6:18 PM, fishina barrel [email protected]
wrote:

means if Granny turns on her hearing aid you have to remember to change
while count < 3
end

Posted via http://www.ruby-forum.com/.

Since we’re sharing, here’s mine:

puts “HI, SONNY!\nIT’S YOUR GRANDMA.”

byes=0

begin
print "> "
reply = gets.chomp
if reply == ‘BYE’
byes += 1
case byes
when 1; puts “CAN’T HEAR YOU, DEAR!”
when 2; puts “STILL CAN’T HEAR YOU, SONNY!”
else ; puts “OKAY, BYE!”
end
else
byes = 0
if reply == reply.upcase
puts “NO, NOT SINCE #{rand(21)+1930}!”
else
puts “EHH?? WHAT'S THAT??”
end
end
end until byes >= 3

Outputs:

HI, SONNY!
IT’S YOUR GRANDMA.

hi gramma
EHH?? WHAT’S THAT??
HI GRAMMA!
NO, NOT SINCE 1939!
HOW’S GRAMPA?
NO, NOT SINCE 1934!
bye
EHH?? WHAT’S THAT??
BYE
CAN’T HEAR YOU, DEAR!
really??
EHH?? WHAT’S THAT??
BYE
CAN’T HEAR YOU, DEAR!
BYE
STILL CAN’T HEAR YOU, SONNY!
BYE
OKAY, BYE!

Just two comments:

Am 22.08.2013 20:33, schrieb matt amaro:

puts “Say something nice to grandma!”
response = nil

the above statement is not necessary

bye = 0

while bye < 3
response = gets.chomp
if response == “BYE”
puts “HUH?! I CAN’T HEAR YOU!”
bye += 1
elsif response == response.upcase
puts “NO! NOT SINCE” + rand(1930…1950).to_s + “!”
bye -= bye

that’s a complicated way of doing `bye = 0’

else
puts “HUH?! I CAN’T HEAR YOU!”
bye -= bye
end
end

Regards,
Marcus

As a noob learning ruby, I am following the Chris P. tutorial for
beginners.

However, the suggestions posted includes a lot of commands we have not
learned at this point of tutorial (Which really confuses me initially).

So here’s my take:

puts ‘Grandma: Hello, its’s been while since you visit me. Come here
and talk to Grandma.’

index = 0

while index != 3

respond = gets.chomp

if respond == ‘BYE’
index = index + 1
else
index = 0
if respond == respond.upcase
puts ‘NO, NOT SINCE ‘+(rand(20)+1930).to_s+’ !’
else
puts ‘HUH?! SPEAK UP, SONNY!’
end
end
end

puts ‘See you again SONNY!’

On Sun, Sep 8, 2013 at 4:41 AM, ChangSeng S. [email protected]
wrote:

puts ‘Grandma: Hello, its's been while since you visit me. Come here
and talk to Grandma.’

If you use double-quotes around the string, you won't, er, I mean,
won’t, need to escape the apostrophe. Some folks say that using
double-quotes ALL the time is less confusing, saving enough
programmer-time to be worth the extra execution-time from Ruby
checking for any string interpolation it needs to do. (See below.)

  puts 'NO, NOT SINCE '+(rand(20)+1930).to_s+' !'

Has the book gotten to string interpolation yet? Long story short,
that’s embedding a variable within a string. You might have seen it
in Perl or shell scripts as something like “Hello, $NAME!”. In Ruby
it’s done with #{}, putting the Ruby expression inside the {}.
However, it requires use of double quotes instead of single. (This
is true in most languages that support string interpolation.) So for
instance, the above would be, in more idiomatic Ruby:

puts “NO, NOT SINCE #{(rand(20)+1930)}!”

-Dave

Was really struggling on this, then found this thread, which helped me
make this work (with repeat BYE req.)

puts “Say something nice to grandma!”
response = nil

bye = 0

while bye < 3
response = gets.chomp
if response == “BYE”
puts “HUH?! I CAN’T HEAR YOU!”
bye += 1
elsif response == response.upcase
puts “NO! NOT SINCE” + rand(1930…1950).to_s + “!”
bye -= bye
else
puts “HUH?! I CAN’T HEAR YOU!”
bye -= bye
end
end

Very short version that functions (without the extra credit part).

puts “Tell Grandma how you are.”
input = gets.chomp
while input != input.upcase
puts "NO, NOT SINCE " + (1930+rand(21)).to_s + “. AND, SPEAK UP!”
input = gets.chomp
end
puts “It was good to see you sonny, now go away now!”

Excellent explanation, Dave. Clean and simple.
On Sep 8, 2013 10:27 AM, “Dave A.” [email protected]

danielj wrote in post #719787:

• Extend your Deaf Grandma program: What if grandma doesn’t want you
to leave? When you shout BYE, she could pretend not to hear you.
Change your previous program so that you have to shout BYE

three times in a row.

Make sure to test your program: if you shout BYE three
times, but not in a row, you should still be talking to grandma.

As far as I checked the programs in this forum don’t solve the ‘3 times
in a row’ requisite. Since I started ruby like ‘yesterday’ I still
haven’t took the time to make it work as required, I got to the same
thing that you did.

Hello everyone!!!

I Think I got it!!! any suggestions to optimize it??
Thanks!

#############################################################
input = gets.chomp
puts ‘HUH! SPEAK UP, SONNY!’

while true
input = gets.chomp

if input < ‘a’ && input != ‘BYE’
puts 'NO, NOT SINCE ’ + (rand(1930…1950)).to_s + ‘!’

elsif (input == ‘BYE’)
bye = 1
while (bye > 0 && bye <3)
input = gets.chomp
bye = bye + 1
end
break
end
end

First of all I would apologize for my bad english. Well its an old topic but i started to read the same book and this is my script for granny:

bye = 0
puts “Ask Grandma something”

while bye < 3 do
input = gets.chomp

if input == input.downcase || input == input.capitalize
puts “HUH?! SPEAK UP SONNY!”
elsif input == input.upcase
puts “NO, NOT SINCE #{rand(1920…1950)}”
end
if input == “BYE”
bye += 1
puts “HEH?”
else
bye = 0
end
end