# Re: Deaf Grandma

**URL:** <https://www.ruby-forum.com/t/re-deaf-grandma/146984>\
**Category:** Ruby\
**Created:** [August 27, 2008, 10:19am UTC](https://www.ruby-forum.com/t/re-deaf-grandma/146984 "2008-08-27T10:19:24Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![stickstone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6f9a4e/32.png) [@stickstone](https://www.ruby-forum.com/u/stickstone)\
**Post date:** [August 27, 2008, 10:19am UTC](https://www.ruby-forum.com/t/re-deaf-grandma/146984/1 "2008-08-27T10:19:24Z")

</div>

> if response == (response.upcase and “BYE”)

That doesn’t make sense. response.upcase is always true (not nil or  
false),  
so (response.upcase and “BYE”) is just the same as “BYE”. Try it in irb  
to  
convince yourself.

There’s also no need to initialise response at the start of the program.  
Apart from that your code looks fine. Here is an alternative version,  
using  
regular expressions and ‘next’ and ‘break’ flow control.

puts “Hey Sonny! It’s your lovely Grandmother! How are yeah?”

bye = 0  
while response = gets  
case response  
when /^BYE$/  
bye += 1  
break if bye \>= 3  
puts “Hmmm… I would prefer…”  
next  
when /[a-z]/  
puts “Huh?! I CAN’T HEAR YOU!”  
when /[A-Z]/  
puts “NO! NOT SINCE #{1930+rand(21)}!”  
end  
bye = 0  
end

(The original problem didn’t say what to do if you said a blank line to  
Granny, or a line which contained no alphabetic characters. In this  
program  
she just ignores you, but still resets the bye counter to zero. This is  
easy  
to change.)

Note there is a subtlety with “gets”. If you ever modify this program to  
take arguments on the command line, “gets” will behave strangely,  
because it  
will treat the command line arguments as filenames to read. For example,

ruby granny.rb 1930 1950

will try to read responses from files called “1930” and “1950”, instead  
of  
from stdin. To make sure you’re always using stdin, you’re better off  
writing

while response = $stdin.gets

(This magic Kernel#gets is a bit of ugliness inherited from Perl.  
Personally  
I find it trips me up rather than being helpful, which is why I mention  
it  
here 🙂

Regards,

Brian.

P.S. At some point you may wish to rewrite this code in a way which can  
easily be unit-tested - and which lets you talk to multiple grannies at  
the  
same time 🙂
