What's wrong with this ruby code? syntax error, unexpected tIDENTIFIER awaiting end_keyword
This code comes from a book called Ruby Best Practice:
def respond_to?(message)
message = message.to_sym
[:__result__, :inspect].include?(message) ||
__result__.respond_to? message
end
But I get the error: syntax error, unexpected tIDENTIFIER awaiting keyword_end. What's the matter?
+3
source to share
1 answer
You will need another parenthesis like
def respond_to?(message)
message = message.to_sym
[:__result__, :inspect].include?(message) ||
__result__.respond_to?(message)
end
or (but looks uglier)
def respond_to?(message)
message = message.to_sym
[:__result__, :inspect].include?(message) ||
(__result__.respond_to? message)
end
In any case, what ruby understands:
def respond_to?(message)
message = message.to_sym
([:__result__, :inspect].include?(message) ||
__result__.respond_to?) message
end
due to operator priority.
I like to call functions without parentheses, but that's fine, only when the code is not ambiguous, ruby does not assign any priority to the newline, as it does for a function ||
.
+3
source to share