John Carter John Carter - 3 months ago 22
Ruby Question

Label Ruby One letter

I am trying to figure this out. I am working on a ruby code where in I want to label each array item where in users can just type 1 letter such as l for lego and so on and it will accept that and validates/check that on the code inside the array.

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


Any idea how to do it? Sorry newbie here.

Answer
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