Deaf grandma

(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 :slight_smile: 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 :slight_smile: 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

···

On Tue, Jul 29, 2008 at 2:06 PM, houston barnett-gearhart <americanpragmatic@gmail.com> wrote:

You will need

class Grandma

end

:slight_smile:

···

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

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!

···

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

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

def grandma

end

:slight_smile:

martin

···

On Tue, Jul 29, 2008 at 2:32 PM, Marc Heiler <shevegen@linuxmail.org> wrote:

You will need

class Grandma

end

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

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

···

On Tue, Jul 29, 2008 at 2:51 PM, houston barnett-gearhart <americanpragmatic@gmail.com> wrote: