Re: Newbie needs help getting user input
- From: 7stud -- <dolgun@xxxxxxxxxx>
- Date: Sun, 30 Sep 2007 11:32:21 +0900
Peter Vanderhaden wrote:
I'm trying to learn Ruby and trying to convert a Perl program at the
same time. I need to prompt a user to enter a number to select a
processing option. In Perl, I did it this way:
#
print STDERR "Enter option: ";
chomp ($option = <STDIN>);
#
print "Enter option (1, 2, 3): "
input = gets.chomp
if input == '1'
puts "I'm executing option 1."
elsif input == '2'
puts "I'm executing option 2."
elsif input == '3'
puts "I'm executing option 3."
else
puts "Bad input."
end
Or:
print "Enter option (1, 2, 3): "
input = gets.chomp
case input
when '1'
puts "I'm executing option 1."
when '2'
puts "I'm executing option 2."
when '3'
puts "I'm executing option 3."
else
puts "Bad input."
end
--
Posted via http://www.ruby-forum.com/.
.
- References:
- Newbie needs help getting user input
- From: Peter Vanderhaden
- Newbie needs help getting user input
- Prev by Date: Re: a = Dog.new # a is not a pointer and not a reference?
- Next by Date: Re: Newbie needs help getting user input
- Previous by thread: Re: Newbie needs help getting user input
- Next by thread: Re: Newbie needs help getting user input
- Index(es):