In Ruby how can I check if a symbol is in an array of symbols? [closed]

0 votes
asked Sep 11, 2017 by chris-f

Can I do the following? If so how?

I have an array of symbols

symbols = %w{:sym1 :sym2 :sym3} # is this correct implementation?

# I'm putting this in a function for this illustration
def check_symbol(symbol)

puts check_symbol(:sym1) # expect true, but I get false
puts check_symbol(:sym44) # expect false of course

How can I do so I get true on the first puts statement?

1 Answer

0 votes
answered Sep 11, 2017 by eugen

You don't have an array of symbols, what you have is an array of strings that look like symbols. The correct definition would be

symbols = %i{sym1 sym2 sym3}

Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter