From: Rick DeNatale Date: 2006-09-10T01:16:56+09:00 Subject: Re: BEST alias_method Re: [CHALLENGE] better alias_method On 9/8/06, ara.t.howard@noaa.gov wrote: > the primary reason to use aliases is not to simply have another handle on > names - but override or wrap a method for instance. Here's a sketch which generalizes this a little and adds a module method sort of like private, which syncronizes the next method to be defined. I'm not sure how you'd make synchronized work like private and make all subsequent methods synchronized. The issue is figuring out when to turn it off when the module definition is closed. rick@bill:/public/rubyscripts$ cat syncwrapper.rb class Module alias_method "__method_added__", :method_added def method_added(meth_name) if @synchronize_new_method @synchronize_new_method = false synchronize_method(meth_name) end __method_added__(meth_name) end def synchronize_method(meth_name) module_eval { alias_method "__#{meth_name}__", "#{meth_name}" } module_eval <<-code def #{meth_name}(*a, &b) p "Synchronized" __#{meth_name}__(*a, &b) end code end def synchronized(*meth_names) @synchronize_new_method = meth_names.empty? meth_names.each do |meth_name| synchronize_method(meth_name) end end end class C def fee 42 end synchronized def fie 43 end def foe 44 end def fum 45 end synchronized :foe end p C.new.fee p C.new.fie p C.new.foe p C.new.fum rick@bill:/public/rubyscripts$ ruby syncwrapper.rb 42 "Synchronized" 43 "Synchronized" 44 45 -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.com/