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
--
Posted via http://www.ruby-forum.com/.
make a choice [enter]
make a choice [enter]
make a choice [q]
Goodbye
please help
the highline library might be of use to you
···
--
Posted via http://www.ruby-forum.com/\.
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()
···
--
Posted via http://www.ruby-forum.com/.
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 Willbanks 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()
--
Posted via http://www.ruby-forum.com/\.
./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
···
--
Posted via http://www.ruby-forum.com/\.
Hi,
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
···
Am Sonntag, 20. Dez 2009, 03:20:33 +0900 schrieb beny 18241:
--
Bertram Scharpf
Stuttgart, Deutschland/Germany
*
Discover String#notempty? at <http://raa.ruby-lang.org/project/step>\.