[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

deaf grandma.

Houston Barnett-gearhart

7/27/2008 3:32:00 AM

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

23 Answers

Martin DeMello

7/27/2008 5:15:00 AM

0

On Sat, Jul 26, 2008 at 8:31 PM, Houston Barnett-gearhart
<americanpragmatic@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/?... 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

7/27/2008 9:32:00 PM

0

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..... 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.
--
Posted via http://www.ruby-....

Siep Korteling

7/27/2008 11:27:00 PM

0

houston barnett-gearhart 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..... 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.

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
--
Posted via http://www.ruby-....

Martin DeMello

7/28/2008 6:09:00 AM

0

On Sun, Jul 27, 2008 at 2:32 PM, houston barnett-gearhart
<americanpragmatic@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..... 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

7/28/2008 6:40:00 AM

0

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: sepp2k@jabber.org
ICQ: 205544826

Houston Barnett-gearhart

7/28/2008 6:51:00 AM

0

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.
--
Posted via http://www.ruby-....

Martin DeMello

7/28/2008 5:46:00 PM

0

On Sun, Jul 27, 2008 at 11:51 PM, houston barnett-gearhart
<americanpragmatic@gmail.com> wrote:
> 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.

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

7/28/2008 8:32:00 PM

0

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!


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

Martin DeMello

7/28/2008 9:47:00 PM

0

On Mon, Jul 28, 2008 at 1:32 PM, houston barnett-gearhart
<americanpragmatic@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

7/29/2008 1:24:00 AM

0

On Sat, Jul 26, 2008 at 10:31 PM, Houston Barnett-gearhart
<americanpragmatic@gmail.com> wrote:
> 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.

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