gpt4 book ai didi

ruby - 如何让用户输入并检查数组中的项目?

转载 作者:太空宇宙 更新时间:2023-11-03 16:52:00 25 4
gpt4 key购买 nike

我希望用户输入他们的 mustache 样式,然后我将根据 mustache 样式数组中的 mustache 列表检查它。这是我想做的额外事情,但我不确定如何解决。我遇到的唯一接近它的东西是 (include?)但我想要:

(数组名).include? (用户输入的值)

require_relative 'list_of'
require_relative 'error'


#User inputs a value
def get_beard
puts "What style of beard do you have?"
beard = gets
if check_beard_ok?(beard)
then no_problem(beard) end
end

#Value passed to check length doesn't exceed 25 letters
def check_beard_ok?(beard)

# while beard.length < 25
# beard.in?(beard_style)
# end
end


#The value is printed here without any errors occuring
def no_problem(beard)
puts "\nYou have a " + beard
puts "\nThere are other beards that you could try like..."
list_of
end

get_beard

最佳答案

看看这个http://www.ruby-doc.org/core-1.9.3/Array.html#method-i-include-3F

你可以做到

breads = [ "Banana bread", "Beer bread", "Pumpernickel" ]
breads_selected = [ "Banana bread"]
if breads.include?(breads_selected)
# this is true
end
unless breads.include?(breads_selected)
# this is false
end

关于ruby - 如何让用户输入并检查数组中的项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19154011/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com