Since we can’t override ‘=’ (and it’s a good idea that we can’t) it seems
that it would be nice to have another overridable operator for assigning
values in classes. So, I’m proposing that we allow a new operator ‘<-’
(less-than minus) which can be overridden to allow assignment, for
example:
class Foo
def initialize(value)
@val = value
end
def <-(newVal)
@val = newVal
end
end
foo = Foo.new(“this value”)
…later…
foo <- “some new value”
Now of course, I could have allowed access to @val by declaring an
attr_accessor, but in many cases I don’t want to expose that information
or make users of the class have to remember the name of the variable
’val’. It just seems much cleaner in many cases to have an assignment
operator. Up till now I’ve been overriding ‘<<’ to do this, but now I’ve
run into a case where I’d like to pass on operators to the underlying
value (@val in the example above) using method_missing so the underlying
value can be shifted (and since ‘<<’ is overriden, that’s kind of tough).
Thoughts?
Phil