[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

a class that looks like (kind_of?) several classes

Eric Mahurin

5/10/2005 4:11:00 PM

Is there a way to make a class that looks like several classes?
For example:

class IOArray < IO
include Array # doesn't work because Array is not a Module
# use IO methods calls to accomplish the Array methods
end

I don't need multiple inheritance. All I want is these
additional classes to appear in the ancestors list. In the
above example, IOArray could act like an IO or an Array. It
looks like evil.rb might have a solution, but is there a
non-evil way?




__________________________________
Do you Yahoo!?
Yahoo! Mail - Helps protect you from nasty viruses.
http://promotions.yahoo.co...


1 Answer

Robert Klemme

5/10/2005 4:31:00 PM

0

Eric Mahurin wrote:
> Is there a way to make a class that looks like several classes?
> For example:
>
> class IOArray < IO
> include Array # doesn't work because Array is not a Module
> # use IO methods calls to accomplish the Array methods
> end
>
> I don't need multiple inheritance. All I want is these
> additional classes to appear in the ancestors list. In the
> above example, IOArray could act like an IO or an Array. It
> looks like evil.rb might have a solution, but is there a
> non-evil way?

class IOArray < IO
def kind_of?(cl)
Array == cl || super
end
end

>> x=IOArray.allocate
=> #<IOArray:0x10184258>
>> x.kind_of? Array
=> true

But seriously, for what do you need that? I can't think of an application
where I would want to make something look like an Array without being one.
Maybe it's rather Enumerable that you want?

Kind regards

robert