# Simple ruby program problem

**URL:** <https://www.ruby-forum.com/t/simple-ruby-program-problem/158409>\
**Category:** Ruby\
**Created:** [January 23, 2009, 12:42am UTC](https://www.ruby-forum.com/t/simple-ruby-program-problem/158409 "2009-01-23T00:42:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![siguva](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c5a1d2/32.png) [@siguva](https://www.ruby-forum.com/u/siguva)\
**Post date:** [January 23, 2009, 12:42am UTC](https://www.ruby-forum.com/t/simple-ruby-program-problem/158409/1 "2009-01-23T00:42:01Z")

</div>

So I have been reading Chris P.'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: [Writing Your Own Methods - Learn to Program](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

---

<div class="post-metadata">

**Author:** ![siguva](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c5a1d2/32.png) [@siguva](https://www.ruby-forum.com/u/siguva)\
**Post date:** [January 23, 2009, 1:03am UTC](https://www.ruby-forum.com/t/simple-ruby-program-problem/158409/2 "2009-01-23T01:03:14Z")

</div>

On Jan 22, 2009, at 6:40 PM, Hunter W. 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 B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![siguva](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c5a1d2/32.png) [@siguva](https://www.ruby-forum.com/u/siguva)\
**Post date:** [January 23, 2009, 1:58am UTC](https://www.ruby-forum.com/t/simple-ruby-program-problem/158409/3 "2009-01-23T01:58:59Z")

</div>

Rob B. wrote:

> On Jan 22, 2009, at 6:40 PM, Hunter W. 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 B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
> removed\_email\_address@domain.invalid

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

---

<div class="post-metadata">

**Author:** ![siguva](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c5a1d2/32.png) [@siguva](https://www.ruby-forum.com/u/siguva)\
**Post date:** [January 23, 2009, 2:15am UTC](https://www.ruby-forum.com/t/simple-ruby-program-problem/158409/4 "2009-01-23T02:15:11Z")

</div>

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

> > be a problem for this program.  
> > Attachments:  
> > [http://www.ruby-forum.com/attachment/3196/post.jpg](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 B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![siguva](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c5a1d2/32.png) [@siguva](https://www.ruby-forum.com/u/siguva)\
**Post date:** [January 23, 2009, 9:01pm UTC](https://www.ruby-forum.com/t/simple-ruby-program-problem/158409/5 "2009-01-23T21:01:23Z")

</div>

Rob B. wrote:

> On Jan 22, 2009, at 7:57 PM, Hunter W. wrote:
> 
> > > be a problem for this program.  
> > > Attachments:  
> > > [http://www.ruby-forum.com/attachment/3196/post.jpg](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 B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
> removed\_email\_address@domain.invalid

You are correct, Rob. Works every time in xterm. Thank you for the  
help.
