Forum: Ruby Deaf Grandma

8834458ffbf0cd4b314a4e16a022d6f2?d=identicon&s=25 danielj (Guest)
on 2008-08-27 02:20
(Received via mailing list)
Also from the Chris Pine tutorial for beginners:

 • Write a Deaf Grandma program. Whatever you say to grandma (whatever
you type in), she should respond with HUH?!  SPEAK UP, SONNY!, unless
you shout it (type in all capitals). If you shout, she can hear you
(or at least she thinks so) and yells back, NO, NOT SINCE 1938! To
make your program really believable, have grandma shout a different
year each time; maybe any year at random between 1930 and 1950. (This
part is optional, and would be much easier if you read the section on
Ruby's random number generator at the end of the methods  chapter.)
You can't stop talking to grandma until you shout BYE.
Hint: Don't forget about chomp! 'BYE'with an Enter is not the same as
'BYE' without one!
Hint 2: Try to think about what parts of your program should happen
over and over again. All of those should be in your while loop.

• 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.


Here is what I came up with:

#Grandma is deaf!

puts "Hey Sonny! It's your lovely Grandmother! How are yeah?"

response = "nope"
bye = 0

while bye < 3
  response = gets.chomp
if response == (response.upcase and "BYE")
  puts "Hmmm... I would prefer..."
  bye = (bye+1)
end
if response != response.upcase
  puts "Huh?! I CAN'T HEAR YOU!"
end
if (response == response.upcase and response != "BYE")
  puts "NO! NOT SINCE " + (1930+rand(21)).to_s + "!"
end
end

Is there an easier way?
Da33a4ac652c1c8900392a8599206640?d=identicon&s=25 Thomas B. (tpreal)
on 2008-08-27 09:17
#Grandma is deaf!

puts "Hey Sonny! It's your lovely Grandmother! How are yeah?"
response = nil
bye = 0
while bye < 3
  response = gets.chomp
  if response == "BYE"
    puts "Hmmm... I would prefer..."
    bye = (bye+1)
  elsif response == response.upcase
    puts "NO! NOT SINCE " + (1930+rand(21)).to_s + "!"
  else
    puts "Huh?! I CAN'T HEAR YOU!"
  end
end
7a561ec0875fcbbe3066ea8fe288ec77?d=identicon&s=25 Sebastian Hungerecker (Guest)
on 2008-08-27 09:42
(Received via mailing list)
danielj wrote:
> if response == (response.upcase and "BYE")

response.upcase and "BYE"  evaluates to "BYE" unless response.upcase
returns
nil, which it won't. So the above is exactly like  if response == "BYE"
8834458ffbf0cd4b314a4e16a022d6f2?d=identicon&s=25 danielj (Guest)
on 2008-08-27 11:05
(Received via mailing list)
Thanks very much for all the help guys.

Programmers seem to be a nice group!
85aef5fe6d432fd862e7ded7a36d1da5?d=identicon&s=25 Chris Dow (grizlord)
on 2009-10-21 01:24
danielj wrote:
> Thanks very much for all the help guys.
>
> Programmers seem to be a nice group!

I rewrote some of it like this:

#Grandma is deaf!

puts "Hey Sonny! It's your lovely Grandmother! How are you?"

response = gets.chomp
bye = 0

while bye < 1
   if response != response.upcase
     puts "Huh?! I CAN'T HEAR YOU!"
   end

   if (response == response.upcase and response != "BYE")
     puts "NO! NOT SINCE " + (1930+rand(21)).to_s + "!"
   end

   if response == "BYE"
     puts "GOOD BYE, SONNY!"
   bye = (bye+1)
   end

response = gets.chomp
end

I'm a noob, but this seems cleaner to me. I know there is a way to write
it with out repeating the "if" statements three times but at least it
works:) The only other thing that bothers me is you have to press enter
at the end to return to the command prompt. If anyone has an example on
how to fix it that would be sweet.
Ee04bc0ca6dcdad4a7e8a8e1d4efb5d0?d=identicon&s=25 Michael W. Ryder (Guest)
on 2009-10-21 02:25
(Received via mailing list)
Chris Mr. wrote:
>
>    end
> it with out repeating the "if" statements three times but at least it
> works:) The only other thing that bothers me is you have to press enter
> at the end to return to the command prompt. If anyone has an example on
> how to fix it that would be sweet.

How about:

#Grandma is deaf!

puts "Hey Sonny! It's your lovely Grandmother! How are you?"

while (response = gets.chomp) != "BYE"
    if response != response.upcase
      puts "Huh?! I CAN'T HEAR YOU!"
    end

    if (response == response.upcase)
      puts "NO! NOT SINCE " + (1930+rand(21)).to_s + "!"
    end
  end
  puts "GOOD BYE, SONNY!"

This only uses one entry of response and no flags.
Ee04bc0ca6dcdad4a7e8a8e1d4efb5d0?d=identicon&s=25 Michael W. Ryder (Guest)
on 2009-10-21 02:30
(Received via mailing list)
Michael W. Ryder wrote:
>> puts "Hey Sonny! It's your lovely Grandmother! How are you?"
>>      puts "NO! NOT SINCE " + (1930+rand(21)).to_s + "!"
>> I'm a noob, but this seems cleaner to me. I know there is a way to
>
>
> This only uses one entry of response and no flags.

I missed the part about removing the multiple if's in the oringal post.

#Grandma is deaf!

puts "Hey Sonny! It's your lovely Grandmother! How are you?"

while (response = gets.chomp) != "BYE"
    if response != response.upcase
      puts "Huh?! I CAN'T HEAR YOU!"
     else
      puts "NO! NOT SINCE " + (1930+rand(21)).to_s + "!"
    end
end
puts "GOOD BYE, SONNY!"
85aef5fe6d432fd862e7ded7a36d1da5?d=identicon&s=25 Chris Dow (grizlord)
on 2009-10-21 08:40
Michael W. Ryder wrote:
> Michael W. Ryder wrote:
>>> puts "Hey Sonny! It's your lovely Grandmother! How are you?"
>>>      puts "NO! NOT SINCE " + (1930+rand(21)).to_s + "!"
>>> I'm a noob, but this seems cleaner to me. I know there is a way to
>>
>>
>> This only uses one entry of response and no flags.
>
> I missed the part about removing the multiple if's in the oringal post.
>
> #Grandma is deaf!
>
> puts "Hey Sonny! It's your lovely Grandmother! How are you?"
>
> while (response = gets.chomp) != "BYE"
>     if response != response.upcase
>       puts "Huh?! I CAN'T HEAR YOU!"
>      else
>       puts "NO! NOT SINCE " + (1930+rand(21)).to_s + "!"
>     end
> end
> puts "GOOD BYE, SONNY!"

That one is sweet. Thanks. I guess I was over thinking it, LOL. I didn't
think of putting the first response in parentheses. This helped me think
of it in a much easier way.
Ae76866a8212a80f2ff777197452a863?d=identicon&s=25 Priten G. (priten_g)
on 2010-12-12 20:03
After reviewing the previous posts, I noticed that many answers did not
incorporate the condition: '...you have to shout BYE three times in a
row...'

This is what I came up with:


puts 'Hey Sonny, it\'s your Grandma! How are you?'

response = nil
bye = 0

while bye < 3
  response = gets.chomp

  if response == 'BYE'
    bye = (bye + 1)

    if bye == 3
      puts 'BYE, SONNY!'
    else
      puts 'HUH?! SPEAK UP, SONNY!'
    end

What do you think?

  elsif response == response.upcase
    puts 'NO! NOT SINCE ' + (1930+rand(21)).to_s + '!'
    bye = 0
  else
    puts 'HUH?! SPEAK UP, SONNY!'
    bye = 0
  end
end
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (candlerb)
on 2010-12-13 10:59
danielj wrote in post #719787:
> Is there an easier way?

Remember to set your bye counter to zero in every case except where
'BYE' is typed, to ensure three BYE's in a row are required. You can use
'next' to skip straight to the top of the next iteration, which means
you can put a single bye=0 at the end of the loop.

Also, I think there is some ambiguity in the original problem
description: what should happen if what you type contains neither upper
nor lower case letters? (e.g. empty text or only punctuation
characters?). In the code below I've let grandma have a snooze. I also
use the 'case' statement to match the response against a series of
values and regexp patterns.

puts "Hey Sonny, it's your Grandma! How are you?"
bye = 0
loop do
  case gets.chomp
  when "BYE"
    bye += 1
    break if bye >= 3
    next
  when /[a-z]/
    puts "HUH?! SPEAK UP, SONNY!"
  when /[A-Z]/
    puts "NO, NOT SINCE #{rand(21)+1930}!"
  else
    puts "ZZZ"
  end
  bye = 0
end

But to be honest, I'd say that if...elsif...elsif...end is equally good.
B31e7abd14f1ceb4c4957da08933c630?d=identicon&s=25 Josh Cheek (josh-cheek)
on 2010-12-13 17:55
(Received via mailing list)
On Mon, Dec 13, 2010 at 3:59 AM, Brian Candler <b.candler@pobox.com>
wrote:

> what should happen if what you type contains neither upper
> nor lower case letters? (e.g. empty text or only punctuation
> characters?).
>

That's called whispering, deaf grandma won't even bat an eye!
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (candlerb)
on 2010-12-14 14:54
Josh Cheek wrote in post #968096:
> On Mon, Dec 13, 2010 at 3:59 AM, Brian Candler <b.candler@pobox.com>
> wrote:
>
>> what should happen if what you type contains neither upper
>> nor lower case letters? (e.g. empty text or only punctuation
>> characters?).
>>
>
> That's called whispering, deaf grandma won't even bat an eye!

You might be swearing at her though...
9f795b9618c720df7289530beac8153e?d=identicon&s=25 Steve K. (steve_k61)
on 2011-05-04 04:22
I was doing stupid things with arrays and all sorts of dumb crap until I
came here and read this thread. Super good work. I wanted my Ruby script
to work with the three goodbyes extension exercise mentioned above.
Also—I like double quotes and #{} to get things done. In my neophyte
opinion, it's cleaner and less characters.

puts "TALK TO ME! SO LONELY!\n\n"
bye = 0
while bye < 3
  response = gets.chomp
  if response == "BYE"
    puts "STAY AWHILE!?!"
    bye = (bye+1)
  elsif response == response.upcase
    puts "NO! NOT SINCE #{1910+rand(41)}!"
    bye = 0
  else
    puts "Huh?! I CAN'T HEAR YOU!"
    bye = 0
  end
end

If you make it a single string—it's 263 if you format it to one line and
strip my OCD new-line characters. It resets "bye" back down to zero if
it's not said three times straight.
15c0a376605fe9e2db0350a2bddba0e4?d=identicon&s=25 Selen Coop (selencoop)
on 2012-09-02 20:44
# Deaf Grandma EXTENDED
rep1 = ''
bye = 0
puts 'HELLO DEAR'
while true
rep1 = gets.chomp
  if rep1 == 'BYE' || rep1 == 'BYE!' || rep1 != rep1.upcase || rep1 ==
rep1.capitalize
  if rep1 == 'BYE' || rep1 == 'BYE!'
    bye = bye+1
    if bye == 3
      break
      end
  else bye = 0
  end
    puts 'HUH?! SPEAK UP SONNY'
  elsif rep1 == rep1.upcase
    bye = 0
      while true
      ry1 = rand(51)
      if ry1 >= 30
        break
      end
    end
    puts 'NO, NOT SINCE 19' + ry1.to_s + '!'
  end
  end
 puts 'OH OK SONNY... COME BACK SOON SONY'
B44c747777cb7bb2b9dfd29f42dc8a35?d=identicon&s=25 Hieu Le (hieulh89)
on 2012-12-11 09:09
puts 'Hi! My name Deaf Grandma'
check = 0
while check <3
if (input = gets.chomp) == input.upcase
                puts "NO! NOT SINCE " + (1930+rand(21)).to_s + "!"
                 if input == 'BYE'
                       check = check + 1
                 else
                       check = 0
                 end

        else
                puts 'HUH?!  SPEAK UP, SONNY!'
        end
end
puts 'Goodbye! See you later!'
6403eff8babdfd0b83b8008974206f97?d=identicon&s=25 hank johnson (bryanus)
on 2013-01-12 05:15
Hieu Le wrote in post #1088624:
> puts 'Hi! My name Deaf Grandma'
> check = 0
> while check <3
> if (input = gets.chomp) == input.upcase
>                 puts "NO! NOT SINCE " + (1930+rand(21)).to_s + "!"
>                  if input == 'BYE'
>                        check = check + 1
>                  else
>                        check = 0
>                  end
>
>         else
>                 puts 'HUH?!  SPEAK UP, SONNY!'
>         end
> end
> puts 'Goodbye! See you later!'

I like this one, but you forgot to reset this check to zero, otherwise
it will end the program if the user inputs 'BYE' 3x non-sequentially
(i.e 'BYE', 'bye', 'BYE, 'BYE')

else
  puts 'HUH?!  SPEAK UP, SONNY!'
  check = 0  #need this extra reset to zero
Fdc233635d91af94b36d2c108b23c57d?d=identicon&s=25 SFCode Feds (sfcodefeds)
on 2013-02-08 19:25
For the full iteration of the problem (upcase, random years & triple BYE
in a row requests), I've got a slightly simpler structure, the code
makes sense to me structurally & intuitively:

puts 'Hello there, Sonny. It\'s your grandma!'
reply = gets.chomp

while reply != 'BYE'
  if reply == reply.upcase
    puts 'NO, NOT SINCE ' +(rand(21)+1930).to_s+ '!'
    reply = gets.chomp
  else
    puts 'HUH?! SPEAK UP, SONNY!'
    reply = gets.chomp
  end
if reply == 'BYE'
  puts 'CAN\'T HEAR YOU, DEAR!'
  reply = gets.chomp
  if reply == 'BYE'
    puts 'STILL CAN\'T HEAR YOU, DEAR!'
    reply = gets.chomp
    if reply == 'BYE'
    end
  end
end
Aa082c8b00a50928e5860dcd70bf2368?d=identicon&s=25 tamouse mailing lists (Guest)
on 2013-02-09 23:43
(Received via mailing list)
On Fri, Feb 8, 2013 at 12:25 PM, Anna Fedorova <lists@ruby-forum.com>
wrote:
>     reply = gets.chomp
>     if reply == 'BYE'
>     end
>   end
> end
>
> --
> Posted via http://www.ruby-forum.com/.
>

Still LOTS of repetition here. DRY: Don't Repeat Yourself, not only
saves typing, and time, but might also lead to better understanding of
algorithms. See if you can write it with only one gets per loop.
Cffbfe01d03966eb524d8bdd76521bea?d=identicon&s=25 fishina barrel (fishinabarrel)
on 2013-02-18 22:34
My solution was very similar to @steve_k61.  Any advice from the peanut
gallery on
making this code cleaner? (Pardon all the comments, it helps me figure
things out.)

#Deaf Grandma w/ extension

#Set initial counter
count=0

#Create Loop Conditions
while count<3
response=gets.chomp

  if response==("bye").upcase
  # Grandma ignores but the count increases by 1
    count+=1
    # (puts count
    # this was an internal test)
  elsif response==response.upcase
  # Grandma respond with random year
    puts ("No, not since ").upcase+(rand(21)+1930).to_s
    count=0
  else
  # Grandma can't hear
    puts ("Huh?! Speak up sonny!").upcase
    count=0
  end

  # Grandma says bye after 3 count
  if count==3
  puts "OK.  Fine.  Just leave then."
  end

end
3df767279ce7d81db0a5bb30f5136863?d=identicon&s=25 Matthew Kerwin (mattyk)
on 2013-02-18 23:17
fishina barrel wrote in post #1097688:
> My solution was very similar to @steve_k61.  Any advice from the peanut
> gallery on making this code cleaner? (Pardon all the comments, it helps
> me figure things out.)

Well, without modifying your basic algorithm, you can trim a lot of
String#upcase calls, and inline a bit of stuff:

  #Deaf Grandma w/ extension

  #Set initial counter
  count=0

  #Create Loop Conditions
  while count < 3
    response=gets.chomp

    if response == 'BYE'
    # Grandma ignores but the count increases by 1
      count+=1
      #puts count  # (this was an internal test)
    elsif response == response.upcase
    # Grandma respond with random year
      puts "NO, NOT SINCE "#{rand(21)+1930}"
      count=0
    else
    # Grandma can't hear
      puts "HUH?! SPEAK UP SONNY!"
      count=0
    end

    # Grandma says bye after 3 count
    if count==3
      puts "OK.  Fine.  Just leave then."
    end

  end

Looks a lot like @steve_k61's code again, huh?

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.
3df767279ce7d81db0a5bb30f5136863?d=identicon&s=25 Matthew Kerwin (mattyk)
on 2013-02-18 23:25
Matthew Kerwin 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?
Cffbfe01d03966eb524d8bdd76521bea?d=identicon&s=25 fishina barrel (fishinabarrel)
on 2013-02-19 01:18
Matthew Kerwin wrote in post #1097695:
> Matthew Kerwin 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.
Aa082c8b00a50928e5860dcd70bf2368?d=identicon&s=25 tamouse mailing lists (Guest)
on 2013-02-19 04:11
(Received via mailing list)
On Mon, Feb 18, 2013 at 6:18 PM, fishina barrel <lists@ruby-forum.com>
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!
Ce88806ce9790880c1f667d87c124954?d=identicon&s=25 matt amaro (mamaro)
on 2013-08-22 20:33
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
15000f55138ae94b0f362ed7c625461a?d=identicon&s=25 unknown (Guest)
on 2013-08-23 20:03
(Received via mailing list)
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
845541ad09875a89d6aaf67ca0c51dab?d=identicon&s=25 ChangSeng S. (changseng_s)
on 2013-09-08 10:41
As a noob learning ruby, I am following the Chris Pine 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!'
F141ba99f29e55f02207f5488fe5ce95?d=identicon&s=25 Dave Aronson (Guest)
on 2013-09-08 16:27
(Received via mailing list)
On Sun, Sep 8, 2013 at 4:41 AM, ChangSeng S. <lists@ruby-forum.com>
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
4f26b34a72729f9543ba84c398dba2c2?d=identicon&s=25 D. Deryl Downey (ddd)
on 2013-09-08 19:37
(Received via mailing list)
Excellent explanation, Dave. Clean and simple.
On Sep 8, 2013 10:27 AM, "Dave Aronson" <rubytalk2dave@davearonson.com>
809cad662695da3d06096ca4ace871e7?d=identicon&s=25 K.R. W. (kr_w)
on 2014-04-04 21:20
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!"
Fa6f59938fed5ea0d5fbafdd58a09165?d=identicon&s=25 Sebastián L. (sebastin_l)
on 2014-06-19 08:33
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.
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.