如何在Ruby中将符号与字符串匹配

bub*_*i93 0 ruby

我有一个包含字符串和符号的数组

在我的函数中,我得到一个字符串来检查数组是否包含该字符串.

array = ["day",:night]

def check(name)
    if array.include? name or array.include? name.to_sym
         return true
    else
         return false
   end
end
Run Code Online (Sandbox Code Playgroud)

如果输入是"day",则返回true.如果输入为"night",则返回false.我希望true在"夜晚"的情况下返回,因为我转换它以检查是否存在具有相同名称的符号.

如何使这个函数工作,以便它将symbol(:night)与string("night")进行比较并返回true

Car*_*and 5

def check(name, array)
  array.map(&:to_s).include?(name.to_s)
end

array = ["day",:night]

check("day", array)   #=> true
check(:day, array)    #=> true
check("night", array) #=> true
check(:night, array)  #=> true
check("cat", array)   #=> false
Run Code Online (Sandbox Code Playgroud)