Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > simple ruby program problem

Reply
Thread Tools

simple ruby program problem

 
 
Hunter Walker
Guest
Posts: n/a
 
      01-22-2009
So I have been reading Chris Pine's book, "Learn to Program". I am
working through the examples and the "a Few Things to Try" sections at
the end of each chapter. I having a problem with one of them.

Here is the chapter: http://pine.fm/LearnToProgram/?Chapter=08

Basically, he wants you to write the psych question program w/o using
the variables good_answer and answer. I tried to do this in the code
below, however, when I run it in TextMate and the gets asks me for input
before puts lists the question. In other words, the first thing that
happens after I run it is the "Script is requesting input" message box.

I will note this: every now and then, the program will work properly,
but this is usually after I run the program for the first after opening
Textmate for the first time.

Thanks for your help!!


# begin code here
def ask question

reply = ''

while (reply != 'yes' || reply != 'no')

puts question

reply = gets.chomp.downcase


if reply == 'yes'
return true

elsif reply == 'no'
return false

end

puts 'Please answer yes or no!'

end

end


puts

ask 'do you like apples?'
wets_bed = ask 'do you wet the bed?'

puts

puts wets_bed

# end of code
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Rob Biedenharn
Guest
Posts: n/a
 
      01-22-2009

On Jan 22, 2009, at 6:40 PM, Hunter Walker wrote:

> while (reply != 'yes' || reply != 'no')



Well, this is effectively and infinite loop becasue reply will always
be != to one or the other and the whole expression will be true.

Since you have a return statement in a couple places, this might not
be a problem for this program.

-Rob

Rob Biedenharn http://agileconsultingllc.com
http://www.velocityreviews.com/forums/(E-Mail Removed)



 
Reply With Quote
 
 
 
 
Hunter Walker
Guest
Posts: n/a
 
      01-23-2009
Rob Biedenharn wrote:
> On Jan 22, 2009, at 6:40 PM, Hunter Walker wrote:
>
>> while (reply != 'yes' || reply != 'no')

>
>
> Well, this is effectively and infinite loop becasue reply will always
> be != to one or the other and the whole expression will be true.
>
> Since you have a return statement in a couple places, this might not
> be a problem for this program.
>
> -Rob
>
> Rob Biedenharn http://agileconsultingllc.com
> (E-Mail Removed)


Yeah, good point. You are right though, the return statement does stop
the loop. See attached screen shot for what happens....

Attachments:
http://www.ruby-forum.com/attachment/3196/post.jpg

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

 
Reply With Quote
 
Rob Biedenharn
Guest
Posts: n/a
 
      01-23-2009

On Jan 22, 2009, at 7:57 PM, Hunter Walker wrote:

> Rob Biedenharn wrote:
>> On Jan 22, 2009, at 6:40 PM, Hunter Walker wrote:
>>
>>> while (reply != 'yes' || reply != 'no')

>>
>>
>> Well, this is effectively and infinite loop becasue reply will always
>> be != to one or the other and the whole expression will be true.
>>
>> Since you have a return statement in a couple places, this might not
>> be a problem for this program.
>>
>> -Rob
>>
>> Rob Biedenharn http://agileconsultingllc.com
>> (E-Mail Removed)

>
> Yeah, good point. You are right though, the return statement does
> stop
> the loop. See attached screen shot for what happens....
>
> Attachments:
> http://www.ruby-forum.com/attachment/3196/post.jpg


Try running directly from an xterm (Terminal) window. I suspect that
running inside TextMate is the cause of the trouble (and that your
script wants input).

-Rob

Rob Biedenharn http://agileconsultingllc.com
(E-Mail Removed)



 
Reply With Quote
 
Hunter Walker
Guest
Posts: n/a
 
      01-23-2009
Rob Biedenharn wrote:
> On Jan 22, 2009, at 7:57 PM, Hunter Walker wrote:
>
>>> be a problem for this program.

>> Attachments:
>> http://www.ruby-forum.com/attachment/3196/post.jpg

>
> Try running directly from an xterm (Terminal) window. I suspect that
> running inside TextMate is the cause of the trouble (and that your
> script wants input).
>
> -Rob
>
> Rob Biedenharn http://agileconsultingllc.com
> (E-Mail Removed)


You are correct, Rob. Works every time in xterm. Thank you for the
help.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Simple program with a simple(?) error -- what's wrong? RichardOnRails Ruby 3 07-21-2008 01:26 PM
looking for a simple way to load a program from another python program.. Eric_Dexter@msn.com Python 10 08-15-2006 05:00 AM
#!/usr/bin/ruby , #!/usr/bin/ruby -w , #!/usr/bin/ruby -T?, #!/usr/bin/ruby -T1... anne001 Ruby 1 04-23-2006 03:02 PM
Simple simple program error...please help tasheeta@gmail.com C++ 14 11-02-2005 12:52 PM
A few simple problems in a simple program. jmac@berkeley.edu C Programming 7 07-23-2003 09:51 PM



Advertisments