Re: Newbie needs help getting user input



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

.