From: dblack@... Date: 2003-03-22T05:44:29+09:00 Subject: Re: Proposal: new operator: '<-' (for assignments) Hi -- On Fri, 21 Mar 2003, Phil Tomson wrote: > 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? This thread may have played out (you have to be awfully quick on the button in ruby-talk these days!), but let me chime in anyway.... I don't think that what you're describing sounds like assignment; that is: foo <- "some new value" isn't actually assigning anything to foo. So it just becomes, in a sense, a kind of status-bestowing (because operator-like) method name, which in the absence of its actually doing assignment I think is potentially misleading. Also, using a regular method name doesn't necessarily mean it has to be named after a particular variable, so concerns about exposing variable names wouldn't have to arise. I'm thinking of things like: def reset_to(x) @val = x end and so on, which don't use the attr_* family of shortcuts but might be more suitable in a given case. David -- David Alan Black home: dblack@superlink.net work: blackdav@shu.edu Web: http://pirate.shu.edu/~blackdav