From: David Alan Black Date: 2001-08-16T07:53:16+09:00 Subject: [ruby-talk:19851] Re: Why not?: Assigning to self Hello -- On Thu, 16 Aug 2001, Jean Michel wrote: > I came upon something which is perhaps an exemple where one might want > to change self, or perhaps just I did not find the right way to achieve > my aim. > > I came upon a situation which could be handled nicely by adding a couple > of methods to the class Range (I will elaborate the situation in a > further message if people are interested): I'd be interested. I don't know why Ranges are immutable, but assuming that we get a convincing explanation, I'd like to see what the best solution to your problem will then be :-) > > Class Range > def shrink_up > @begin+=1 > end > def shrink_down > @end-=1 > end > end > > Well, this does not work since begin and end are not writable > attributes (nor attributes at all, in fact). The next try would be :) > > Class Range > def shrink_up > self=(begin+1..end) > end > def shrink_down > self=(begin..end-1) > end > end I don't know if this helps in your situation, but you could do a sort of faux version of it: class Range def shrunk_up(n=1) (first + n .. last) end def shrunk_down(n=1) (first .. last - n) end end r = (0..10) p r.shrunk_up(3) # => 3..10 p r.shrunk_down # => 0..9 It's a typing-saver, at best, but maybe useful. David -- David Alan Black home: dblack@candle.superlink.net work: blackdav@shu.edu Web: http://pirate.shu.edu/~blackdav