[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

about delegator

uncutstone

4/25/2006 2:08:00 AM

I have a question about ruby's delegate mechanism.

Ruby supports automatic delegate by inherited from DelegateClass or
SimpleDelegator. But what if I want a class can inherit from some other
class but still support automatic delegate. Since ruby doesn't support
multi inheritance, it seems no way.

Why automatic delegate cannot be supported by module instead of class?

Thanks in advance.

1 Answer

uncutstone

4/25/2006 2:46:00 AM

0

I rethink the problem and realize module cannot be used to implement
automatic delegate, because automatic delegate is based on automatic
call super.

I already get a solution if i want inherit from other class and also
need automatic delegation. I list the code below:

class Song
attr_reader :duration, :artist, :name
def initialize(name, artist, duration)
@name = name
@artist = artist
@duration = duration
end
def bye
puts "goodbye: #{name}, #{artist}, #{duration}"
end
end

class SongDelegator
def initialize(aSong)
@aSong= aSong
end
def method_missing(methId)
str = methId.id2name
@aSong.send str
end
end

aSong = Song.new('f', 'i', 1200)
aSongDelegator = SongDelegator.new(aSong)
aSongDelegator.bye

It is easy to do. The core is redefine method_missing.