[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: subclassing fundamental classes

Dan Zwell

5/8/2007 6:35:00 AM

xyz wrote:
> Let's say I have a user-defined class as follows:
>
> class C1
> #-------------------------
> def initialize(i, j)
> @iv1 = Array.new(i, j)
> end
> #-------------------------
> def append(i)
> @iv1 << i
> return(self)
> end
> #-------------------------
> def append2X(i)
> @iv1 << 2 * i
> return(self)
> end
> #-------------------------
> end
>
> Now I subclass C1 to make C2:
>
> class C2 < C1
> #-------------------------
> def initialize(inA)
> @iv1 = inA
> end
> #-------------------------
> def append(i)
> @iv1 << i << i
> return(self)
> end
> #-------------------------
> def doubleLast()
> @iv1[-1] *= 2
> return(self)
> end
> #-------------------------
> end
>
> The internal state of the superclass (C1) is visible to methods in the subclass
> (C2) and has a known name that can be used in the subclass (@iv1). Now let's
> look at a similar situation but where the superclass is one of the "fundamental"
> classes pre-packaged with Ruby (i.e. Array, Hash, String, etc.). A "fundamental"
> class is one that is not constructed from some other Ruby class using Ruby
> source but is instead constructed from the same underlying machinery as the
> interpreter itself (in the current implementation this means C source). In the
> following "???" denotes a name for an instance variable in the superclass.
>
> class C3 < Array
> def initialize(inArray)
> ??? = inArray
> end
> def append(i)
> ??? << i << i
> return(self)
> end
> def join(i)
> out = ""
> sep = "#" * i
> ???.each { |e| out << sep + e.to_s + sep }
> return(out)
> end
> end
>
> What do I use in place of the ???'s above? That is, what name do I use in the
> subclass (C3) for the instance state of a fundamental superclass (Array)?
>
> When a superclass is fundamental, can I access instance state in said superclass
> without having to resort to the underlying C interface?
>
> In my specific case, I am using instances of Array to hold data in a particular
> format (the array elements are Hash's whose keys are String's of a particular
> format and whose values are Hash's of a particular format). I want to subclass
> Array, add a couple of methods, override a couple of methods, and also use the
> non-overridden methods of Array.
>
> It looks like I could make this work by simply adding my new methods directly to
> Array and using alternate (if less descriptive) names for the Array methods I
> was planning on overriding (this would allow me to access the internal state of
> Array using self). However this approach will make the code slightly less
> readable since some Array variables will be generic Array's and some will be my
> special version. And to make things worse, I actually have two special versions
> of Array and was planning on constructing two separate subclasses.
>
> Thanx.
>
> Jake
>
>

I think it's just "self", but you'll have one problem because you can't
assign a value to self. I would try:

def initialize(inArray)
inArray.each {|elem| self << elem}
end

Dan