Let’s say I have a Ruby class:
class MyClass def self.property return 'someVal' end def self.property=(newVal) # do something to set 'property' success = true return success # success is a boolean end end
If I try and do MyClass.property=x, the return value of the whole statement is always x. It is a convention in a lot of C-based/inspired languages to return a boolean ‘success’ value – is it possible to do this for a setter using the ‘equals syntax’ in Ruby?
Furthermore – if this isn’t possible, why not? Is there any conceivable downside to allowing an ‘equals setter’ operation return a value?
One downside is that you would break the chained assignment semantics:
Consider:
Then
xwould taketrueif this worked as you had expected (right-associativity). That could be a surprise for people using your interface and used to the typical semantics.You also got me thinking about parallel assignment, eg:
Apparently the return value from that expression is implementation specific… I guess I won’t be chaining parallel assignments 🙂
Nice question!