标签Ruby一封信

时间:2016-09-07 09:18:03

标签: ruby

我想弄明白这一点。我正在处理一个ruby代码,在这里我要标记每个数组项,用户可以在其中键入1个字母,例如l for lego等等,它将接受并验证/检查数组内的代码。< / p>

MY_CHOICES = %w(lego violin xray)

l = "lego"
v = "violin"
x = "xray"

print "Choose one: #{MY_CHOICES.join(', ')} "
choice = gets.chomp

if MY_CHOICES.include?(choice)
  break
else
  puts "That is not included there."
end

知道怎么做吗?对不起新手在这里。

1 个答案:

答案 0 :(得分:1)

MY_CHOICES = {
  'l' => 'lego',
  'v' => 'violin',
  'x' => 'xray'
}

print "Choose one: #{MY_CHOICES.values.join(', ')} "
choice = gets.chomp

if MY_CHOICES[choice]
  puts "#{MY_CHOICES[choice]} chosen"
else
  puts "That is not included there."
end