我正在制作吉他浏览CLI,我希望能够将用户输入转换为整数。我知道它是to_i
,但我只需要执行它并将其与我的数据关联的帮助。这是我关注的CLI类的一部分:
while (input = gets.strip.downcase) != 'exit'
case input
when "electric"
puts ""
display_electric
end
end
def display_electric
HiStrung::Guitar.electrics.each.with_index(1) do |electric, index|
puts "#{index}. #{electric.name}".red.bold + " - #{electric.url}".light_white
end
end
[如果用户输入electric
,他们将返回不同吉他的列表,然后他们将输入1
或5
作为他们想进一步了解的吉他。这是了解电吉他列表的外观:
1. Fender Telecaster - https://reverb.com/marketplace/electric-guitars?query=telecaster
2. Gibson Les Paul - https://reverb.com/marketplace?query=les%20paul
3. Fender Stratocaster - https://reverb.com/marketplace/electric-guitars?query=stratocaster
4. Gibson SG - https://reverb.com/marketplace?query=sg
5. Fender Jazzmaster - https://reverb.com/marketplace?query=jazzmaster
朋友,也许您可以做这样的事情。
mylist = %w{fender, gibson, ibanez}
mylist.each_with_index do |guitar, index|
if index == 0 # this zero means i want fender!
puts guitar
end end