I’m trying to add a property called “enabled?” to a model with both a getter and a setter. However, when I do the following:
def enabled?= value
# .. logic goes here ..
end
I get syntax error, unexpected '?', expecting '\n' or ';'
What should I be doing instead?
Yes, ruby syntax only allows
?in method names if it’s the last character, sofoo?=is not valid. One thing you could do, that would be pretty idiomatic, is to defineenabled?,enableanddisable(orenable!anddisable!if you want to emphasize that they’re mutating methods).If that doesn’t fit your needs, you can just name the methods
enabled?andenabled=and just live with the slight incosistency between the names.