From: Pit Capitain Date: 2005-04-07T16:06:23+09:00 Subject: Re: how to simulate self-style delegation [SuperHash] itsme213 schrieb: > "Pit Capitain" wrote in message > >> class Person >> def delegate_to(role) >> @seam.clear if @seam >> @seam = Seam.new do |s| >> s.instead_of(self, :describe) do role.describe end >> s.instead_of(role, :name) do self.name end >> end >> end >> end > > Interesting. I'll give it some thought. > > Would this require knowledge of the internal structure of role#describe? Or > would it be safe to do the following? > (s.methods - Object.instance_methods).each {|m| > s.instead_of(role, m.intern) do self.send(m) end > } You have to consider message parameters and blocks. If you want to pass them unchanged to the other object you could do it this way: (s.methods - Object.instance_methods).each do |m| s.instead_of(role, m) do |c| self.send(m, *c.args, &c.block) end end The seam block is called with one parameter, a call object. Via this call object you can get access to the original method call, for example at its parameters and the block passed. Regards, Pit