Francis Hwang <sera / fhwang.net> wrote in message news:<a05100300b9113bf8a60a@[10.0.1.18]>...
> I'm trying to subclass Array, and I'm finding it would be useful to 
> be able to use concatenation operators like "+" and "+=". I'm not 
> sure how to make it work, though.
> 
> The concatenation operators seem to leave your object as an Array, 
> not the previous subclass. For example:
> -----
> class MyClass < Array
>    ...
> end
> 
> object = MyClass.new
> object << 1
> # object is still type MyClass
> object = object + [2, 3, 4]
> # object is now type Array
> ------
> Any hints as to how I might retain the type?

This might be easier if you used simple delegator to handle the array
like behavior, then wrapped the addition methods like

require 'delegate'
class MyClass < SimpleDelegator
    def initialize
       super([])
    end

    def + other
        __getobj__ += other
        self
    end
end

This is a quick guess..

~ Patrick