0

I thought this should work:

def setMethod(method)
  if method in @@methods
    ... do something
  end
end

But I keep getting keyword error for in

lurker
  • 55,215
  • 8
  • 64
  • 98
Mohamed El Mahallawy
  • 12,072
  • 13
  • 47
  • 81
  • 2
    You get an error because that's not how you check inclusion in Ruby. The error message is telling you true: there is no keyword `in` in Ruby. Use `@@methods.include? method` instead. See, for example, http://stackoverflow.com/questions/1986386/check-if-value-exists-in-array-in-ruby. Sometimes the way we expect a language should work isn't the way it works. ;) – lurker Sep 08 '14 at 16:16
  • Awesome, thanks! I was just looking at python code and converting it to ruby and coffeescript has the `in` keyword and claims to try to 'give ease to ruby developers who want to write coffeescript' – Mohamed El Mahallawy Sep 08 '14 at 16:22
  • Interesting. Yeah, that's probably a little misleading on their part. – lurker Sep 08 '14 at 16:24
  • Ruby does have an `in` keyword that can be used for `for` loops, but it is hardly used. – David Grayson Sep 08 '14 at 16:30

1 Answers1

3

Try this, assuming @@methods is an array:

if @@methods.include?(method)
  # ...
end
David Grayson
  • 79,096
  • 23
  • 144
  • 182