uncutstone
4/25/2006 2:46:00 AM
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.