Creating a setter method that takes extra arguments in Ruby Creating a setter method that takes extra arguments in Ruby ruby ruby

Creating a setter method that takes extra arguments in Ruby


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] = expression and expression.[]= arguments and 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 =.


I have come across this and decided to pass my arguments as an array or hash.

E.g.:

def allow=(arguments)  puts arguments[:letter]  puts arguments[:position]  puts arguments[:allowed]endobject.allow={:letter=>'A',:position=>3,:allowed=>true}