[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

ASCII Menu in Ruby, is it possible?

Ruby Student

2/19/2009 7:47:00 PM

[Note: parts of this message were removed to make it a legal post.]

Hello Team,

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.

Thank you

--
Ruby Student

8 Answers

Jason Roelofs

2/19/2009 7:58:00 PM

0

Did you try anything or just jump straight here to ask?

puts "1. Do something here"
puts "2. I want this"
puts "3. Hmm"

x = gets

case x
when "1"
...
when "2"
...
when "3"
...
end

Unless you're not including enough detail for a proper answer, a quick
jaunt through any Ruby book/tutorial/website would get you at least
this far.

Jason

On Thu, Feb 19, 2009 at 2:47 PM, Ruby Student <ruby.student@gmail.com> wrote:
> Hello Team,
>
> 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.
>
> Thank you
>
> --
> Ruby Student
>

Justin Collins

2/19/2009 8:05:00 PM

0

Ruby Student wrote:
> Hello Team,
>
> 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.
>
> Thank you
>
>
Sure you can.

For example:

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
end

menu

-Justin

Eleanor McHugh

2/19/2009 8:12:00 PM

0

On 19 Feb 2009, at 19:47, Ruby Student wrote:
> Hello Team,
>
> 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.
>
> Thank you

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.


Ellie

Eleanor McHugh
Games With Brains
http://slides.games-with-...
----
raise ArgumentError unless @reality.responds_to? :reason



Ruby Student

2/19/2009 8:23:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

On Thu, Feb 19, 2009 at 3:12 PM, Eleanor McHugh <
eleanor@games-with-brains.com> wrote:

> On 19 Feb 2009, at 19:47, Ruby Student wrote:
>
>> Hello Team,
>>
>> 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.
>>
>> Thank you
>>
>
> 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.
>
>
> Ellie
>
> Eleanor McHugh
> Games With Brains
> http://slides.games-with-...
> ----
> raise ArgumentError unless @reality.responds_to? :reason
>
>
>
>

Eleanor,

Thank you for your help. It is highly appreciated!
--
Ruby Student

Ruby Student

2/19/2009 8:24:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

On Thu, Feb 19, 2009 at 3:04 PM, Justin Collins <justincollins@ucla.edu>wrote:

> Ruby Student wrote:
>
>> Hello Team,
>>
>> 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.
>>
>> Thank you
>>
>>
>>
> Sure you can.
>
> For example:
>
> 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
> end
>
> menu
>
> -Justin
>
>

Dear Jason,

YES, I did first performed a google search, which is this:

"ruby" "menu" "ASCII menu"

and the above gave me this:
http://www.google.com/search?hl=en&q=%22ruby%22+%22menu%22+%22ASCII+menu%22&start=3...

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.

Again, thank you!
--
Ruby Student

James Gray

2/19/2009 8:33:00 PM

0

On Feb 19, 2009, at 1:47 PM, Ruby Student wrote:

> 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:

http://github.com/JEG2/highline/blob/664a1f9d9d7e850d2e72c87ed8594ee910367624/example...

James Edward Gray II

Igor Pirnovar

2/20/2009 12:04:00 AM

0

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!

> STDOUT.sync = true

#!/usr/bin/env ruby

STDOUT.sync = true # (cross-platform compatibility issue)

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 <Enter> 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 <Enter> 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 <Enter> to continue "
any = gets
end
end

--
Posted via http://www.ruby-....

Ruby Student

2/20/2009 1:44:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

On Thu, Feb 19, 2009 at 7:03 PM, Igor Pirnovar <gooigpi@gmail.com> wrote:

> 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!
>
> > STDOUT.sync = true
>
> #!/usr/bin/env ruby
>
> STDOUT.sync = true # (cross-platform compatibility issue)
>
> 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 <Enter> 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 <Enter> 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 <Enter> to continue "
> any = gets
> end
> end
>
> --
> Posted via http://www.ruby-....
>
>

I truly appreciate the abundance of suggestions from everyone. I will try
every suggestion and pick one!

Thank you
--
Ruby Student