-2

What is the correct syntax to check something like this?

if A IN (:B, :C ) {
 do something
}

I get syntax error but what is the proper way to do this in Ruby?

SyntaxError: unexpected ',', expecting ')'
lacostenycoder
  • 10,623
  • 4
  • 31
  • 48
sofs1
  • 3,834
  • 11
  • 51
  • 89
  • 1
    `IN` is neither a keyword nor a method. `if` statements to not have blocks. `(:B, :C)` is not valid code. `A` is a constant whose value has not been given. You may want something like `if [:B, :C].include?(:A); ; end`. – Cary Swoveland Oct 29 '19 at 17:59
  • Does this answer your question? [Check if a value exists in an array in Ruby](https://stackoverflow.com/questions/1986386/check-if-a-value-exists-in-an-array-in-ruby) – Edin Omeragic Oct 29 '19 at 18:18
  • "But it is not working" is not a precise enough error description for us to help you. *What* doesn't work? *How* doesn't it work? What trouble do you have with your code? Do you get an error message? What is the error message? Is the result you are getting not the result you are expecting? What result do you expect and why, what is the result you are getting and how do the two differ? Is the behavior you are observing not the desired behavior? What is the desired behavior and why, what is the observed behavior, and in what way do they differ? – Jörg W Mittag Oct 29 '19 at 22:40
  • 1
    _"I have a normal if condition"_ – it's anything but normal. Ruby doesn't use curly braces for `if` statements. There are no uppercase operators in Ruby, there's no `IN` operator and there are no stand-alone `(:B, :C)` lists in Ruby, either. Maybe you're coming from another programming language, but you can't expect some arbitrary syntax to work – of course it is not working. – Stefan Oct 30 '19 at 07:24

1 Answers1

1

Your syntax is not valid ruby code. To check if an item is in a collection, use Enumerable#.include?

if [:B, :C].include?(:A)
  # do something
end
lacostenycoder
  • 10,623
  • 4
  • 31
  • 48