On 5/16/07, Dan Z. firstname.lastname@example.org wrote:
I just want Pairs#sort_by to return a Pairs object every time it is
called. Could anybody help me?
This should solve your block passing problem.
However, I’d strongly suggets rethinking making a subclass of array
In most cases its better to use delegation to an array instance
# 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
# argument as Array so …
@array = array
# 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)
self.array = other_array.to_ary # use to_ary instead of to_a
# to_ary is used for internal conversions of objects
# For a method which was replaced in your subclass just replace
# explicit (or implicit) self with @array
My blog on Ruby