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

4
  • 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, stackoverflow.com/questions/1986386/…. Sometimes the way we expect a language should work isn't the way it works. ;) Commented Sep 8, 2014 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' Commented Sep 8, 2014 at 16:22
  • Interesting. Yeah, that's probably a little misleading on their part. Commented Sep 8, 2014 at 16:24
  • Ruby does have an in keyword that can be used for for loops, but it is hardly used. Commented Sep 8, 2014 at 16:30

1 Answer 1

3

Try this, assuming @@methods is an array:

if @@methods.include?(method)
  # ...
end
Sign up to request clarification or add additional context in comments.

2 Comments

The question wasn't clear but they might have wanted this... for method in @@methods or better yet @@methods.each do |method|
No it's an if statement just like with python

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.