Is there a way in Ruby to create primitive ASCII menus?
At this point I am shying away from GUI as everything outthere, in the
GUI
world is too…
I just want to know if I can create simple menus of the type:
1 - Enter 1 and press Enter to continue
2 - Enter 2 or X and press Enter to exit
3 - Etc.
You can find an example of doing this with the termios library by
following the link in my sig and reading the Camping presentation. For
more advanced text-based GUIs you could look at ncurses and highline.
def menu
loop do
puts “1. Do something”, “2. Do something else”, “3. Nevermind”
input = gets.strip
case input
when "1"
puts "Did something"
when "2"
puts "Did something else"
when "3"
puts "Bye!"
return
else
puts "Invalid option: #{input}"
end
end
As a routine, I religiously perform first a good search. I also traverse
the
gems repository. Then I come to the forum.
That been said, I appreciate your answer and justin answer. I truly
appreciate your help.
It was far from my mind using gets to perform this task.
Is there a way in Ruby to create primitive ASCII menus?
At this point I am shying away from GUI as everything outthere, in
the GUI
world is too…
I just want to know if I can create simple menus of the type:
1 - Enter 1 and press Enter to continue
2 - Enter 2 or X and press Enter to exit
3 - Etc.
Highline can build these menus for you. See examples here:
If you are planning to do some data entry checking on Linux platforms
you may need to set the STDOUT.sync = true. I am including a simple
program to show you the difference. Indeed on MS Windows you will not
experience any difference if you comment out “STDOUT.sync = true”; on
Linux if buffering is turned on you will!
loop do
print “\n\n\tPLEASE SELECT:\n\n”
print “\t\t(1) … enter one number\n”
print "\t\t(2) … enter two numbers "
print “(no seoarators like commas, …)\n”
print “\t\t(Q) … Quit\n\n”
print "\tPlease select one of the above: "
answer = gets
printf “You’ve selected %s\n”, answer
case answer.chomp
when “1”
num = nil
while num !~ /\d+.[^\d]/
print "Please enter a single number: "
num = gets
num.chomp! # note exclamation mark (!)
if num.split(/\s+/).size != 1|| num !~ /\d+/
puts “You should have entered a single number not [#{num}]”
print "Press to continue "
any = gets
else
puts “Thank you for [#{num}].”
end
end
when “2”
num = nil
while num !~ /\d+[\s,]+\d+/
print "Please enter two numbers: "
num = gets
num.chomp! # note exclamation mark (!)
if num.split(/\s+/).size != 2 || num !~ /\d+\s+\d+/
puts “You must enter two numbers number, not [#{num}]”
print "Press to continue "
any = gets
else
puts “Thank you for [#{num}].”
end
end
when /q|Q/
exit
else
print "Illegal selection; Please try again! "
print “Please enter a single number:\n”
print "Press to continue "
any = gets
end
end