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.
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.
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.
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."
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.
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.
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!"
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.
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.
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.
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!
···
On Mon, Feb 18, 2013 at 6:18 PM, fishina barrel <lists@ruby-forum.com> wrote: