From: Mathieu Bouchard Date: 2001-01-31T13:27:55+09:00 Subject: [ruby-talk:10157] Re: ArrayMixin 0.2 On Wed, 31 Jan 2001, David Alan Black wrote: > On Wed, 31 Jan 2001, Mathieu Bouchard wrote: > > Second "release" of ArrayMixin 0.2, a future equivalent to Perl's TIEARRAY > > system. > Can anyone help me out with an example or problem where Ruby (as it > currently is) does not already allow us to do what we might do with > Perl tie, with about the same amount of work? (Perl: define STORE, > Ruby: (re)define #[]=, etc.) I seem to be absolutely unable to see > it. In Perl you don't need to redefine the array slice. You don't need to implement the complete semantics of what appears to be a store. When you do: $blah[1] = 2 what is being done is really: tied(@blah)->STORE(1,2) tied(@blah)->FETCH(1) In Ruby, #[]= has more features yet than Perl's assignments, and Perl already abstracts out its assignment operations, so I told myself: why not in Ruby? ArrayMixin is designed to minimise the effort on the implementor of an Array-like class, by taking that concept of Perl (tie) and refining it. If you are willing to reimplement the complete functionality #[]= and #[] by yourself every time, would you please send me a sample implementation that I could include in ArrayMixin. Thank you. ----------------8<----------------cut-here----------------8<---------------- package Foo; sub TIEARRAY { my ($class) = @_; my $self = bless {}, $class; return $self;} sub STORE { my ($self,$i,$v) = @_; print "at: $i put: $v.\n";} sub FETCH { return 42;} 1; ----------------8<----------------cut-here----------------8<---------------- Perl> tie @blah, 'Foo' Foo=HASH(0x80c2990) Perl> $blah[1] = 2 at: 1 put: 2. 42 Perl> @blah[1..3] = (1,4,9) at: 1 put: 1. at: 2 put: 4. at: 3 put: 9. 3 ----------------8<----------------cut-here----------------8<---------------- matju