I’m trying to write a method that acts as a setter and takes some extra arguments besides the assigned value. Silly example:
class WordGenerator
def []=(letter, position, allowed)
puts "#{letter}#{allowed ? ' now' : ' no longer'} allowed at #{position}"
end
def allow=(letter, position, allowed)
# ...
end
end
Writing it as an indexer works and I can call it like this:
gen = WordGenerator.new
gen['a', 1] = true
# or explicitly:
gen.[]=('a', 1, true)
But when I try any of the following, the interpreter complains:
gen.allow('a', 1) = false # syntax error
gen.allow=('a', 1, false) # syntax error
Why won’t this work, am I missing the obvious?
It doesn’t work because the parser doesn’t allow it. An equals sign is allowed in expressions of the form
identifier = expression,expression.identifier = expression(where identifier is\w+),expression[arguments] = expressionandexpression.[]= argumentsand as part of a string or symbol or character literal (?=). That’s it.gen.send(:allow=, 'a', 1, false)would work, but at that point you could as well just give the method a name that doesn’t include a=.