Dan Zwell
5/16/2007 9:27:00 PM
Thanks, that worked immediately.
Dan
Mark Day wrote:
> On May 16, 2007, at 1:49 PM, Dan Zwell wrote:
>
>> I have subclassed Array to add some features. I am redefining sort_by
>> so that it does not return an Array but a Pairs (my subclass), but I
>> cannot get the syntax for passing the blocks right.
>>
>> Here's my code:
> <snip>
>> alias_method :sort_by_old, :sort_by
>> # PROBLEM here:
>> def sort_by(&block)
>> ary = self.sort_by_old {yield(block)}
>> Pairs.new(ary)
>> end
>
> Use this instead:
> ary = self.sort_by_old(&block)
>
> >> p=Pairs["abcd", "efg", "hi", "j"]
> => ["abcd", "efg", "hi", "j"]
> >> p.class
> => Pairs
> >> q=p.sort_by{|x| x.length}
> => ["j", "hi", "efg", "abcd"]
> >> q.class
> => Pairs
>
> -Mark
>
>
>