simple one

B

beny 18241

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
 
S

Simeon Willbanks

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()
 
B

beny 18241

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 said:
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()
 
R

Roger Pack

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

Bertram Scharpf

Hi,

Am Sonntag, 20. Dez 2009, 03:20:33 +0900 schrieb beny 18241:
i used both methods and they works (thanks for that), but i have ine
more question (both methods are affected)

---
#!/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.
...

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top