Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

deaf grandma.

0 views
Skip to first unread message

Houston Barnett-gearhart

unread,
Jul 26, 2008, 11:31:49 PM7/26/08
to
i picked up ruby 2 days ago & have been bustlin' chris pine's "learn to
program" tutorial, which goes over various aspects of ruby on a very
fundamental level. up until today, i've had relatively no problem
working out the exercises. but, as i expected, i've run into my first
significant obstacle. the exercise is as follows, "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."

now, i have a pretty good idea which methods i should be using to have
grandma respond with "huh!? speak up, sonny!" whenever something is said
that is not in all capitals & i've got a firm grasp on the fact that for
the "any year at random between 1930 & 1950" i'll need to use the rand
method & define it as rand(21) + 1930. i've just been toying around with
the little i know & haven't been able to get the result i want. i
promise you i've toyed around with branching, looping, etc., but am
still not able to get even remotely close to what the exercise asks of
me.

if anyone's able to get me started in the right direction, i'd
appreciate it. there's also an extension to the exercise & it states,
"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." but, i'll worry about that after i've got
the previous stuff squared away. thanks in advance.
--
Posted via http://www.ruby-forum.com/.

Martin DeMello

unread,
Jul 27, 2008, 1:14:50 AM7/27/08
to
On Sat, Jul 26, 2008 at 8:31 PM, Houston Barnett-gearhart
<american...@gmail.com> wrote:

> if anyone's able to get me started in the right direction, i'd
> appreciate it. there's also an extension to the exercise & it states,
> "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." but, i'll worry about that after i've got
> the previous stuff squared away. thanks in advance.

Here's an iterative approach to solving this:

0. Write an echo program. Ask the user for some input, repeat it back
and exit. E.g.

$ ruby grandma.rb
Please say something: Hello world
You said: Hello world

1. Set up an echo loop: ask the user for some input, then say it back,
then repeat those two steps endlessly

2. Write an expression that returns 'true' if a string is in all caps
and false otherwise

3. Have your program only echo back if the input is in all caps

4. Add the "huh? i can't hear you!" if the input is not in all caps

Look up the section titled "Branching" in
http://pine.fm/LearnToProgram/?Chapter=06 if you have trouble with
steps 3 and 4

5. Now go back to the code that says what to do if grandma *can* hear
you, and add your random number code

It might be helpful to write your program out as an algorithm first
(i.e. forget about syntax, just write out the flow of logic. For
example, this is the Mrs Gabbard code from chapter 6 written out as an
algorithm:

Introduce yourself as Mrs Gabbard
Ask the student for his/her name
See if the name is properly capitalised
---If it is
------Ask the student to take a seat
---Otherwise
------Work out the properly capitalised form
------Ask if that is what the student meant
------If the answer is 'yes'
---------Say "take a seat"
------If not
---------Say "get out!"

martin

houston barnett-gearhart

unread,
Jul 27, 2008, 5:32:07 PM7/27/08
to
thank you, martin, for your reply. i took your advice & wrote an
algorithm & it's been helping me along quite well. but, i've a few
questions that arose while i was writing the program. before i get
started, here's the program thus far: http://pastie.org/242167. i
realize that the code is a mess & i probably did much more than was
necessary, but like i said, i'm fairly new to ruby, let alone
programming. by all means, please suggest how i could clean it up a bit.
now, for my questions. foremost, after grandma's spiel about it being so
long since she last saw you & how mother said you had a question for
her, if you ask the question & you capitalize any letter in your
question, the program ends. why is that? second, visually, when the
program is running, it's not particularly fun to look at. i figured
centering the text would make a bit more pleasing to the eye, but not so
much. is there a string method that enables you to output whatever
grandma is saying as if she is typing it in real time? if so, how can i
implement that in my program?

that's the gist of my questionnaire. it's been frustrating, but as i
figure things out by myself or through the hints people give me, i get a
feeling of accomplishment. i understand that i'm just starting out & i
shouldn't expect to have an elite knack to code in ruby, but i've been
at this program on & off for 2 days & i'm not really getting all the
results i've wanted. there's still a lot i need to work in.
specifically, grandma's response if you give her initial question an
answer that is not "yes," "Yes," "no," or "No," on top of the extension
to the program, which is a whole other monster for the time being.

again, any help is appreciated.

Siep Korteling

unread,
Jul 27, 2008, 7:26:46 PM7/27/08
to

Well. you have a whole lot of "if" statements. An if statement has a
something which is true or false; in your case

reply1 == "yes"

If it's true, the code behind it will run. If it's false, the code after
an else-statement will run. If there is no "else" , the if statement
will ignore all code until it finds it's "end" statement.

In your case that means all following code is skipped. The "end"s are
misplaced.

You might want to look into "while" or "until" to tackle this.

childs_reply = ""
until childs_reply == "BYE"
childs_reply = gets.chomp
##do smart stuff
#if
#else
#end
end

Regarding grandma's typing. She's going to do this all the time, so
let's make a method. Basically we want to recieve a string, take each
letter, print it and then pause a little.

def grandma_says(str) #str is the stuff we're going to show.
str.each_byte do |byte| #Whoa. each_byte? Never mind, it's there to
print byte.chr #handle non-western character sets.
sleep 0.1
STDOUT.flush #major pain in the ass, this (on windows).
end
puts #forces a line ending when grandma is
finished.
STDOUT.flush
end

grandma_says("I may be old but I'm not slow!")

Hth,

Siep

Martin DeMello

unread,
Jul 28, 2008, 2:09:23 AM7/28/08
to
On Sun, Jul 27, 2008 at 2:32 PM, houston barnett-gearhart
<american...@gmail.com> wrote:
> thank you, martin, for your reply. i took your advice & wrote an
> algorithm & it's been helping me along quite well. but, i've a few
> questions that arose while i was writing the program. before i get
> started, here's the program thus far: http://pastie.org/242167. i
> realize that the code is a mess & i probably did much more than was
> necessary, but like i said, i'm fairly new to ruby, let alone
> programming. by all means, please suggest how i could clean it up a bit.

I looked at your code, and it looks like you're taking an inside-out
approach to the problem; that is, you're concentrating on the little
bits and then trying to fit them into some sort of overall structure.
Particularly since you're learning to code, I'd strongly recommend
leaving that code alone, and starting from scratch using a more
top-down approach. Specifically, get the *structure* of your program
correct first, then slowly fill in that structure to have all the
features you need. Go back and check out the iterative approach I
suggested in my last post, where you build up the code feature by
feature starting with the basic question-answer-question-answer....
loop.

After you've been at it a while, this will become second nature to
you, and you'll be able to keep the structure of the program in your
head and fill in details even before you have the whole skeleton
mapped out, but that takes experience, and unnecessarily complicates
matters at this stage. Also, when you come to chapter 8 in the
tutorial you'll learn about methods, which are a powerful way to keep
your program well-structured throughout its development.

martin

Sebastian Hungerecker

unread,
Jul 28, 2008, 2:39:46 AM7/28/08
to
houston barnett-gearhart wrote:
> is there a string method that enables you to output whatever
> grandma is saying as if she is typing it in real time? if so, how can i
> implement that in my program?

def type(string, speed=5)
old_sync = $stdout.sync
$stdout.sync = true
string.each_char do |c|
print c
sleep 1.0/speed
end
$stdout.sync = old_sync
end

type "foo\nbar\bz"

This will type 5 letters per second (the default value for speed), first
typing foo, then a newline, then bar and then deleting the r in bar and
replacing it with a z.
So put this method definition at the beginning of your script and then use
type instead of puts. If you're using ruby 1.8.6, you'll also have to
require 'jcode' at the top of the file.

HTH,
Sebastian
--
Jabber: sep...@jabber.org
ICQ: 205544826

houston barnett-gearhart

unread,
Jul 28, 2008, 2:51:05 AM7/28/08
to
Martin, is there any way I can get in touch with you outside of the
forum? Preferably through a medium that is as close to real-time as
possible, i.e., Skype, Gmail's chat function, etc. I appreciate the help
you've given me thus far & would like to be able to bounce my progress
off you as I work on the exercise, so as to be able to ascertain whether
or not I'm on the right track. This will undoubtedly help me
significantly, alongside being a bit more time efficient, considering I
spent a considerable amount of time on the code I posted & I was on the
wrong track from the get-go. I think it'd be easier this way, not to
mention space & time efficient.

Martin DeMello

unread,
Jul 28, 2008, 1:45:50 PM7/28/08
to

I'm a bit too busy at work for realtime stuff these days - but have
you discovered the ruby IRC channel? It's #ruby-lang on
irc.freeenode.net, and there are lots of helpful, friendly people on
there.

martin

houston barnett-gearhart

unread,
Jul 28, 2008, 4:32:11 PM7/28/08
to
I don't know why this is so hard for me to get my head around, but
thing's just aren't working out as I'd planned. Martin, I've followed
your iterative approach, but I can't seem to implement each specificity
into a conglomerate program. I have discovered #ruby-lang & I use it
quite frequently, but it's much more difficult to explain the problems
I'm having when I need to explain, in detail, what it is I'm doing, what
I've tried, etc. In large part, that difficulty pushed me to posting
here. Now, because you've said you're busy with work & unable to fiddle
with any real-time stuff, I'll continue to post my questions here. As
for the iterative approach & what I've written for each:

0.

puts
puts "Please say something."
puts
reply = gets.chomp
puts
puts "You said #{reply}."

1.

loop {puts "Please say something."
puts
reply = gets.chomp
puts}

2.

puts
puts "The world is flat. True or false?"
puts
reply = gets.chomp.downcase
if reply == "true"
puts
puts "You must've read Thomas L. Friedman's book!"
elsif reply == "false"
puts
puts "Ah, submitting to Pythagoras' proposal that"
puts "the world is round, I see. That's so 2,508"
puts "years ago!"
end

3. (I cannot take ownership for writing this. sepp2k from #ruby-lang
helped me considerably. He alerted me to the fact I shouldn't be using
go-to style flow control & told me looping was the better bet.)

reply = "abc"
while reply != reply.upcase
puts
puts "Simon says, 'Input your text in all"
puts "caps! Otherwise, I won't echo back"
puts "anything you say! Instead, I will"
puts "spit out my 'Simon says' spiel once"
puts "more. & you wouldn't want that!"
puts
reply = gets.chomp
end

puts reply

.. for now, that's all I've got. I've been working on each individual
step of the iterative approach & trying to pick up what I can through my
questions on #ruby-lang. I'll post the code for 4 & 5 later on in the
day. Please take a look over what I've written (with the help of an
array of people) & let me know if there's anywhere I can clean it up.
Thanks!

Martin DeMello

unread,
Jul 28, 2008, 5:46:59 PM7/28/08
to
On Mon, Jul 28, 2008 at 1:32 PM, houston barnett-gearhart
<american...@gmail.com> wrote:
>
> 3. (I cannot take ownership for writing this. sepp2k from #ruby-lang
> helped me considerably. He alerted me to the fact I shouldn't be using
> go-to style flow control & told me looping was the better bet.)

This is actually shaping up very well. Here's a comment:

> reply = "abc"
> while reply != reply.upcase
> puts
> puts "Simon says, 'Input your text in all"
> puts "caps! Otherwise, I won't echo back"
> puts "anything you say! Instead, I will"
> puts "spit out my 'Simon says' spiel once"
> puts "more. & you wouldn't want that!"
> puts
> reply = gets.chomp
> end
>
> puts reply

The algorithm for this is:

loop
---- get a reply
---- if the reply is not in all caps
------ repeat loop
---- otherwise
------ exit loop

You're not doing anything with the reply yet. The following skeleton
might be easier to work with

0 loop
1 ---- get a reply
2 ---- if the reply is in all caps
3 -------- do something
4 ---- otherwise
5 -------- do something else
6 ---- repeat loop

Note that steps 1-5 simply get a reply and deal with responding to it,
and that they are contained cleanly inside the loop body. Of course,
you do want to exit the loop at some point, but I'd suggest doing it
this way:

0 loop
1 ---- get a reply
2 ---- if the reply is in all caps
3 -------- do something
4 ---- otherwise
5 -------- do something else
6 ---- break out of the loop if we need to
7 ---- repeat loop (unless we've broken out)

Ruby does support the 'break' call to exit a loop, but another, and
often helpful way to do things is to have a variable act as a flag,
and check the flag each time to see whether we loop again. Then,
within the body of the loop, you signal "ready to exit" by setting the
flag. Here's some code that demonstrates both approaches:

# one way: use a flag to end the loop
done_talking = false
while (not done_talking) do
puts "Loop 1> Say something (or 'quit' to exit)"
reply = gets.chomp
if (reply.downcase == "quit")
done_talking = true
else
if (reply == reply.upcase)
puts "that was in all caps"
else
puts "that was not in all caps"
end
end
end

puts "exited loop 1"

# and the other way: loop infinitely, but break out if needed
while true do
puts "Loop 2> Say something (or 'quit' to exit)"
reply = gets.chomp
if (reply.downcase == "quit")
break
else
if (reply == reply.upcase)
puts "that was in all caps"
else
puts "that was not in all caps"
end
end
end

puts "exited loop 2"

# an advantage of the 'break' method is that it saves one level of if/else
while true do
puts "Loop 3> Say something (or 'quit' to exit)"
reply = gets.chomp
if (reply.downcase == "quit")
break
end

if (reply == reply.upcase)
puts "that was in all caps"
else
puts "that was not in all caps"
end
end

puts "exited loop 3"


>
> ... for now, that's all I've got. I've been working on each individual


> step of the iterative approach & trying to pick up what I can through my
> questions on #ruby-lang. I'll post the code for 4 & 5 later on in the
> day. Please take a look over what I've written (with the help of an
> array of people) & let me know if there's anywhere I can clean it up.
> Thanks!

No problem :) Looking forward to seeing the next two parts.

martin

Todd Benson

unread,
Jul 28, 2008, 9:24:13 PM7/28/08
to

I haven't looked at Pine's specific question, but I'd be a little
surprised if it's this simple...

s = some_string; s.upcase == s

Todd

Todd Benson

unread,
Jul 28, 2008, 9:27:48 PM7/28/08
to
On Mon, Jul 28, 2008 at 8:24 PM, Todd Benson <cadu...@gmail.com> wrote:
>
> I haven't looked at Pine's specific question, but I'd be a little
> surprised if it's this simple...
>
> s = some_string; s.upcase == s

BTW, that doesn't give you a direct answer to your whole question, but
may be what Pine was looking for the _shout_ part.

Todd

houston barnett-gearhart

unread,
Jul 29, 2008, 2:30:39 AM7/29/08
to
Martin, I believe I've successfully written the first portion of Chris
Pine's "Deaf Grandma" exercise. With the help of the stuff you posted
about the various approaches to take to break a loop & various people on
#ruby-lang (particularly Giraph), I've fulfilled the majority of my
goal, which turned out to be quite a frustrating task. Although the text
being outputted suggests that you are speaking to Grandma through an
intermediary (at first), the main idea is still heavily rooted in what
is said & how user input is replied to. This is the finished program, &
as you can tell, I've implemented various things from the suggestions
given in posts on here & in #ruby-lang. I'm most proud of the fact that
it's as if Grandma & her intermediary are talking to you in real-time...
the effect is nice. So, here's the code for the finished program (void
of the extension, which I will work on tomorrow):

==========

def granny_says(str)
str.each_byte do |byte|
print byte.chr
sleep 0.05
STDOUT.flush
end
puts
STDOUT.flush
end

while true do
granny_says("Got a question for granny? Make sure to")
granny_says("input yo' question in all caps, or else")
granny_says("granny can't hear ya'!\n\n")
reply = gets.chomp
if (reply == reply.upcase)
random = rand(21) + 1930
granny_says("\nNo, not since #{random}!")
granny_says("That's all the time I've got, child!")
granny_says("Hasta la-bye-bye!")
break
else
granny_says("\nGranny can't hear you! Wanna know why?")
granny_says("'Cause that wasn't in all caps, yo'!")
granny_says("So, I'll repeat what I said before ...\n\n")
end
end

==========

Now that I've got that squared away, I'll look into adding the extension
to the exercise. & to tell you the truth, I've no idea how to do that.
I'm thinkin' I need to create another loop, & define (some how, I'm
still not sure) that Grandma will only say goodbye after the user has
inputted "bye" three times in a row. I guess I have an inkling of an
idea of how to go about it, just not sure it'll work. If all goes well,
I'll be posting later tomorrow with the total exercise, extension
included, complete. Wish me luck!

houston barnett-gearhart

unread,
Jul 29, 2008, 2:35:59 AM7/29/08
to
Oh, guess I should have checked the forum before I said I wasn't sure
how to go about writing the extension. It looks like Phee got further
than I did without much help (good for him, really), but got tangled up
on what I would end up inevitably getting tangled up in myself. With the
replies from his topic, I'm bound to be able to write the extension &
have the total package done by tomorrow morning or afternoon.

Martin DeMello

unread,
Jul 29, 2008, 4:48:57 PM7/29/08
to
On Mon, Jul 28, 2008 at 11:30 PM, houston barnett-gearhart
<american...@gmail.com> wrote:
> the effect is nice. So, here's the code for the finished program (void
> of the extension, which I will work on tomorrow):

It looks good :) And as you note in your next post, the extension
doesn't really need another loop, just something within the same loop
that adds an additional check for whether to exit or not.

martin

houston barnett-gearhart

unread,
Jul 29, 2008, 5:06:23 PM7/29/08
to
Martin, regardless of having been away I don't need another loop, I seem
to have added one anyway. Now, I've satisfied every requirement the
exercise ascertained. But, I've been told that the indentation is funky
& that the print statements could be shortened. Most significant is the
fact that I seem to have "one loop too many," but I'm unsure of where it
is & how to fix it. Here's the URL to the code on Pastie
(http://pastie.org/243587).

I'd say it's about 99% complete. Although I do need the following, as
mentioned above:

• uniform indentation;
• shortened print statements;
• one less loop

If you could suggest how I go about doing any of the above, or throw
some URLs my way that speak on the topics of indentation & the like, I'd
appreciate it. Once I get your suggestions (or toy around more with the
code) & I've re-worked everything, I'd say we're golden.

Martin DeMello

unread,
Jul 29, 2008, 5:23:32 PM7/29/08
to
On Tue, Jul 29, 2008 at 2:06 PM, houston barnett-gearhart
<american...@gmail.com> wrote:
> (http://pastie.org/243587).
>
> I'd say it's about 99% complete. Although I do need the following, as
> mentioned above:
>
> • uniform indentation;

This is something a good editor should do for you. If you let us know
what platform you are on, people can recommend their favourite editors
and IDEs :) I use gvim myself; below is an example of how it
autoindents your code:

http://pastie.org/243603

A heavyweight but very nicely polished option is netbeans with ruby
support: http://wiki.netbeans.org/Ruby

> • shortened print statements;

Not sure what you mean by that. If you have a lot of text to print, it
needs to be included, after all :) There are things you could do to
compact the code but they add little value at this stage - I'd say go
ahead and do it the straightforward way as you have here.

> • one less loop

There's a problem with your code as written - note that once you've
said "bye" you can never say anything else - it sticks in the loop
till it counts three 'byes' in a row. Whereas what you want is to exit
the whole loop if you see three byes in a row, otherwise just carry on
as normal. What you *should* do is this -

if the user has yelled, so that granny hears what he says, do an
additional check for his having said "bye".
---- If that is true, then increase the bye count.
-------- If the bye count is 3, exit the loop
---- If that is false, reset the bye count to zero and carry on with
the rest of the code

Hope that helps!

martin

Marc Heiler

unread,
Jul 29, 2008, 5:32:26 PM7/29/08
to
You will need

class Grandma

end

:)

Martin DeMello

unread,
Jul 29, 2008, 5:45:05 PM7/29/08
to
On Tue, Jul 29, 2008 at 2:32 PM, Marc Heiler <shev...@linuxmail.org> wrote:
> You will need
>
> class Grandma
>
> end

Actually, if you listen to the problem statement, he wants

def grandma

end

:)

martin

houston barnett-gearhart

unread,
Jul 29, 2008, 5:51:10 PM7/29/08
to

> There's a problem with your code as written - note that once you've
> said "bye" you can never say anything else - it sticks in the loop
> till it counts three 'byes' in a row. Whereas what you want is to exit
> the whole loop if you see three byes in a row, otherwise just carry on
> as normal. What you *should* do is this -
>
> if the user has yelled, so that granny hears what he says, do an
> additional check for his having said "bye".
> ---- If that is true, then increase the bye count.
> -------- If the bye count is 3, exit the loop
> ---- If that is false, reset the bye count to zero and carry on with
> the rest of the code

Martin, this keeps being brought up. If I'm understanding correctly,
after the Intermediary explains that questions towards Granny should be
in all caps if you want her to hear you, you want to additionally check
the inputted text (if it's in all caps) to check if "BYE" has been said.
But, logically that doesn't make any sense. Why would the first thing
you say to Granny, after it's been suggested you ask a question, be
"BYE"? It doesn't make sense to me.. which is why I believe I put the
loop where it lays currently. Of course, I could be completely
misunderstanding what you're saying. Which is more likely! If I am, just
clarify & I'll begin working on what you've been trying to say. At it's
written, it works as I want it to. It is still heavily rooted in Chris
Pine's exercise & covers most, if not all, of the requirements. The one
thing that I believe is most significant is the loop issue, which I hope
to have sorted out later today if all goes well.

>If you let us know what platform you are on, people can recommend their >favourite editors and IDEs

I'm on Windows XP. Recommend away!

Martin DeMello

unread,
Jul 31, 2008, 3:01:15 AM7/31/08
to
On Tue, Jul 29, 2008 at 2:51 PM, houston barnett-gearhart
<american...@gmail.com> wrote:
>
> Martin, this keeps being brought up. If I'm understanding correctly,
> after the Intermediary explains that questions towards Granny should be
> in all caps if you want her to hear you, you want to additionally check
> the inputted text (if it's in all caps) to check if "BYE" has been said.
> But, logically that doesn't make any sense. Why would the first thing
> you say to Granny, after it's been suggested you ask a question, be
> "BYE"? It doesn't make sense to me.. which is why I believe I put the

What you're missing is that, because it's a loop, the same code gets
executed for every question. Consider the following code

1 loop do
2 granny_says("<granny> say something")
3 reply = gets.chomp
4 if (reply == reply.upcase)
5 granny_says("<granny> i hear you loud and clear")
6 else
7 granny_says("<granny> eh? speak up!!")
8 end
9 end

Now consider the transcript

<granny> say something
hello
<granny> eh? speak up!!
<granny> say something
i said "hello"
<granny> eh? speak up!!
<granny> say something
"hello"!!!!!!!
<granny> eh? speak up!!
<granny> say something
HELLO!
<granny> i hear you loud and clear
<granny> say something

Here it is again, annotated with the line of code being run

1
2 <granny> say something
3 hello
4
6
7 <granny> eh? speak up!!
2 <granny> say something
3 i said "hello"
4
6
7 <granny> eh? speak up!!
2 <granny> say something
3 "hello"!!!!!!!
4
6
7 <granny> eh? speak up!!
2 <granny> say something
3 HELLO!
4
5 <granny> i hear you loud and clear
2 <granny> say something

Now if the user wants to say 'bye', which lines of code will handle
it? Note that the only place where the program prompts for user input
is lines 2 and 3. So we run line 2

2 <granny> say something

and wait for user input

3 BYE

the program will go to line 4, then to line 5, then skip 6,7,8 (the
else block) and go to line 9 where it loops again. So the place to
process the user's input and check if he has said "bye" is inside the
if block. Ideally, instead of

-- say "i hear you loud and clear"

you want

-- did he say "bye"?
---- was it the third time in a row?
------- if so, exit
------- if not, pretend you didn't hear

so you want (1) an inner if/else block to replace line 5 and (2) logic
within that block to keep track of whether we've seen three
consecutive 'bye's

The problem with using a separate loop is this:

<granny> say something
"hello"!!!!!!!
<granny> eh? speak up!!
<granny> say something
HELLO!
<granny> i hear you loud and clear
<granny> say something
BYE
<granny> i'm afraid i didn't hear you properly - it almost sounded
like you said 'bye'
<granny> say something
okay, I'm not leaving

Now what? If you're inside a "wait for three byes" loop, you have no
way of handling something that is *not* a BYE. So you need to have a
single loop whose overall structure is

loop do
-- ask for input
-- do something with input
-- check if we need to exit
---- if so, exit
---- if not, make a response
end

And use variables for any tracking you need to do that spans multiple
repeats of the loop

>>If you let us know what platform you are on, people can recommend their >favourite editors and IDEs
>
> I'm on Windows XP. Recommend away!

I'm on linux, so no real recommendations, I'm afraid. See if
http://rubyonwindows.blogspot.com/2007/05/what-text-editor-should-i-use.html
has anything helpful to suggest. Also, people's requirements vary; for
me, autoindentation is the key feature of a good programming editor,
followed by decent syntax highlighting.

martin

Jason Morgan

unread,
Feb 18, 2009, 1:39:38 AM2/18/09
to
I've been reading Chris Pine's book the last couple of days. I spent
most the day today stuck on this deaf Grandma thing.

This is what i finally came up with hope it helps;

count = 1

while count != 4
response = gets.chomp

if response == 'BYE'
count = count + 1
else
count = 1
end

if response == response.upcase && response != 'BYE'
puts 'Just like in ' + (rand(30) + 1920).to_s
end

if response == response.downcase
puts 'HUH?! SPEAK UP, SONNY!'
end
end

7stud --

unread,
Feb 18, 2009, 2:34:10 AM2/18/09
to
Jason Morgan wrote:
> I've been reading Chris Pine's book the last couple of days. I spent
> most the day today stuck on this deaf Grandma thing.
>
> This is what i finally came up with hope it helps;
>

Finally! After 8 long months of checking this thread every day, someone
posts a solution. Thank you, thank you, thank you.

Dave Lilley

unread,
Mar 19, 2009, 5:50:20 AM3/19/09
to

Sorry for the interuption but you may find the RubyLearning Free online
course useful.

3 months duration no fee only your time and commitment to join
discussions, try writing programs and call for help when stuck.

url is www.rubylearning.com

sorry to those who i might offend

dave.

0 new messages