From: "ara.t.howard" Date: 2008-05-06T23:25:55+09:00 Subject: Re: delegation question, where I want prototype style delegation On May 6, 2008, at 12:00 AM, Sam Roberts wrote: > But it has a problem, it modifies the target object, but I may want > to create mutiple delegates to the > target, each with a different set of "views", that behave like the > target object would if it > had its #each method redefined. you can play with this: cfp:~ > cat a.rb class Base instance_methods.each do |m| unless m[%r/^__/] old = m new = "__#{ old }__" new << '?' if new.sub!('?', '') new << '!' if new.sub!('!', '') alias_method new, old undef_method old end end def initialize object @object = object end def method_missing m, *a, &b @object.__send__ m, *a, &b end Delegates = {} def / m Delegates[m] ||= __dup__.__instance_eval__ do extend m self end end end module Reverse def each *a, &b reverse.each *a, &b end end a = Base.new [2,4] p a.each{} p (a/Reverse).each{} cfp:~ > ruby a.rb [2, 4] [4, 2] a @ http://codeforpeople.com/ -- we can deny everything, except that we have the possibility of being better. simply reflect on that. h.h. the 14th dalai lama