> I'd strongly suggets rethinking making a subclass of array > In most cases its better to use delegation to an array instance > variable. > > Something like: > > require 'forwardable' > > class Pairs > extend Forwardable > > # For methods which you would not have overriden in the subclass > # simply delegate to the instance variable @array > # this is just an example, the division between methods which are > # simply delegated, which are replaced and which are wrapped is up > # to you. > def_delegators(:@array, :[], :[]=, :+ #... > > # I notice that your initialize method doesn't seem to take the same > # argument as Array so ... > def initialize(array) > @array = array > end > =begin > # If you wanted to have the same protocol for creating your class > # as Array then you'd need something like this. > def initialize(*args, &block) > @array = Array.new(*args, &block) > end > > def self.[](*objs) > new().replace(Array[*objs]) > end > =end > > def replace(other_array) > self.array = other_array.to_ary # use to_ary instead of to_a > end > > # to_ary is used for internal conversions of objects > def to_ary > array > end > > # For a method which was replaced in your subclass just replace > # explicit (or implicit) self with @array > def concat(other_arr) > array.concat(other_arr.to_pairs) > end > > def sort_by(&b) > Pairs.new(@array.sort_by(&b)) > end > > def to_pairs > self > end > > end > > class Array > def to_pairs > Pairs.new(self) > end > end > > > Thanks for the code examples--I previously didn't understand exactly how delegation worked. This looks neat. I'll be playing with this and will most likely use it. Thanks again, Dan