# Simple one

**URL:** <https://www.ruby-forum.com/t/simple-one/180372>\
**Category:** Ruby\
**Created:** [December 18, 2009, 5:21pm UTC](https://www.ruby-forum.com/t/simple-one/180372 "2009-12-18T17:21:16Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![beny18241](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbce88/32.png) [@beny18241](https://www.ruby-forum.com/u/beny18241)\
**Post date:** [December 18, 2009, 5:21pm UTC](https://www.ruby-forum.com/t/simple-one/180372/1 "2009-12-18T17:21:16Z")

</div>

Hi,

As i wanted to make some kind of test.I have one simple question

in example below, if user puts enter , he will recive info “make a  
choice”  
How to make if user push enter twice he will recive second info “make a  
choice” and still there will be availabilty to choose between q and y  
options?

* * *

if $choice == ‘’ || $choice == nil  
puts “\nmake a choice\n”

```
    else if $choice == 'q'
            print "\nGoodbye\n"

    else if $choice == 'y'

```

## else put “blablabla” end

see output that i wish to recive

make a choice [enter]  
make a choice [enter]  
make a choice [q]  
Goodbye

please help

---

<div class="post-metadata">

**Author:** ![beny18241](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbce88/32.png) [@beny18241](https://www.ruby-forum.com/u/beny18241)\
**Post date:** [December 19, 2009, 2:28am UTC](https://www.ruby-forum.com/t/simple-one/180372/2 "2009-12-19T02:28:24Z")

</div>

> make a choice [enter]  
> make a choice [enter]  
> make a choice [q]  
> Goodbye
> 
> please help

the highline library might be of use to you

> **[Ruby Highline – Examples](https://raveendran.wordpress.com/2009/08/01/ruby-highline-examples/)**
>
> Examples   Installation: \>gem install highline In Code -\> Include — \> require “highline/import” Basic usage: ask(“Company? “) { |q| q.default = “no…

---

<div class="post-metadata">

**Author:** ![beny18241](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbce88/32.png) [@beny18241](https://www.ruby-forum.com/u/beny18241)\
**Post date:** [December 19, 2009, 2:38am UTC](https://www.ruby-forum.com/t/simple-one/180372/3 "2009-12-19T02:38:30Z")

</div>

I’ve written a simple program which I believe meets your requirements.  
I’ve changed the text a bit, but I hope the code is helpful.

Simeon

class Choice  
def initialize  
@choices = [‘y’, ‘N’]  
@instructions = ‘make a choice ‘+ @choices.join(’/’)  
@response = ‘you have choosen %s’+"\n"+‘Goodbye’  
end  
def ask()  
puts @instructions  
STDOUT.flush  
@input = gets.chomp  
self.respond()  
end  
def respond()  
if @choices.include?(@input)  
puts @response % @input  
STDOUT.flush  
else  
self.ask()  
end  
end  
end

c = Choice.new  
c.ask()

---

<div class="post-metadata">

**Author:** ![beny18241](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbce88/32.png) [@beny18241](https://www.ruby-forum.com/u/beny18241)\
**Post date:** [December 19, 2009, 7:20pm UTC](https://www.ruby-forum.com/t/simple-one/180372/4 "2009-12-19T19:20:31Z")

</div>

Hi

i used both methods and they works (thanks for that), but i have ine  
more question (both methods are affected)

see my code (example)

* * *

#!/usr/bin/ruby

```
def ask(prompt)
    loop do
        print prompt, ' '
        $stdout.flush
        s = gets
        exit if s == nil
        s.chomp!
        if s == 'y' or s == 'yes'
            return true
        elsif s == 'n' or s == 'no'
            return false
        else
            $stderr.puts "Please answer yes or no."
        end
    end
end

```

## ask(’ =\> ')

i Saved it as a file.rb, as there is a small part of my bigger scirpt ,  
i wanted to add argument, so i’am running it with argument(which is used  
later in my script):

./file.rb file.xml

and i recive  
=\> Please answer yes or no.  
=\> Please answer yes or no.  
=\> Please answer yes or no.  
=\> Please answer yes or no.  
=\> Please answer yes or no.  
=\> Please answer yes or no.  
=\> Please answer yes or no.  
…

I know that I should clear input or something like that, but i dont know  
how , I’ ve tried many ways.

Please help

Regards  
beny18241

Simeon W. wrote:

> I’ve written a simple program which I believe meets your requirements.  
> I’ve changed the text a bit, but I hope the code is helpful.
> 
> Simeon
> 
> class Choice  
> def initialize  
> @choices = [‘y’, ‘N’]  
> @instructions = ‘make a choice ‘+ @choices.join(’/’)  
> @response = ‘you have choosen %s’+"\n"+‘Goodbye’  
> end  
> def ask()  
> puts @instructions  
> STDOUT.flush  
> @input = gets.chomp  
> self.respond()  
> end  
> def respond()  
> if @choices.include?(@input)  
> puts @response % @input  
> STDOUT.flush  
> else  
> self.ask()  
> end  
> end  
> end
> 
> c = Choice.new  
> c.ask()

---

<div class="post-metadata">

**Author:** ![beny18241](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbce88/32.png) [@beny18241](https://www.ruby-forum.com/u/beny18241)\
**Post date:** [December 20, 2009, 7:57pm UTC](https://www.ruby-forum.com/t/simple-one/180372/5 "2009-12-20T19:57:14Z")

</div>

Hi,

Am Sonntag, 20. Dez 2009, 03:20:33 +0900 schrieb beny 18241:

> ```
> s = gets
> end
> 
> ```
> 
> and i recive  
> =\> Please answer yes or no.  
> =\> Please answer yes or no.  
> =\> Please answer yes or no.  
> …
> 
> I know that I should clear input or something like that, but i dont know  
> how , I’ ve tried many ways.

There must be a loop around the `ask’ method.

By the way, why don’t you use `case’?

def ask prompt  
loop do  
print prompt, ’ ’  
$stdout.flush  
s = gets or break  
s.chomp!  
case s  
when /^y/i, “ja”, “oui” then break true  
when /^n/i then break false  
else $stderr.puts “Please answer yes or  
no.”  
end  
end  
end

ask ’ =\>’

Bertram

---

<div class="post-metadata">

**Author:** ![beny18241](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbce88/32.png) [@beny18241](https://www.ruby-forum.com/u/beny18241)\
**Post date:** [December 20, 2009, 3:59am UTC](https://www.ruby-forum.com/t/simple-one/180372/6 "2009-12-20T03:59:07Z")

</div>

> ./file.rb file.xml
> 
> and i recive  
> =\> Please answer yes or no.  
> =\> Please answer yes or no.  
> =\> Please answer yes or no.  
> …

That repeats forever?

Perhaps gets is reading from file.xml? (I don’t know why but maybe it  
is?)  
-r
