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

标签 ruby arrays search input

我希望用户输入他们的 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/

相关文章:

ruby-on-rails - Rake 由于序列无效而中止

ruby-on-rails - 强参数 - Devise 3.0.0 和 Rails 4。 "Unpermitted parameters: name"

c++ - 我怎样才能在 C++ 中打印一个 char 数组

java - 如何在使用字节数组更改后将图像字节数组转换为位图

java - 为什么Java不提供标准函数来反转数组?

ruby - 这可能是 Jekyll 中的错误吗? ('endfor' 未知标签)

java - 500,000 个街道名称——使用什么数据结构来实现快速搜索?

java - IMAP 搜索命令的替代方法

c++ - 列表类型的binary_search函数的时间复杂度是多少

ruby-on-rails - Rails - 4.2.1 #<ActiveRecord::Relation []> 的未定义方法 `or'