Ilmari Heikkinen
2/2/2006 2:42:00 PM
On 2/1/06, Damphyr <damphyr@freemail.gr> wrote:> What are my alternatives?Here's a thought:class Wrapper instance_methods.grep(/^[^_]{2,2}/).each{|im| undef_method im } attr_accessor :handlers def initialize(obj) @handlers ||= Hash.new{|h,k| h[k] = []} @obj = obj end def method_missing(mname, *args, &block) if mname.to_s[0,3] == "on_" @handlers[mname.to_s[3..-1]] << [block, args] else @handlers[mname.to_s].each{|handler, hargs| handler.call(*(hargs+[mname]+args)) } @obj.send(mname, *args, &block) end endendw = Wrapper.new("foo")#=> "foo"w.on_reverse{ puts "reversed!" }w.on_split{|m,splitter| puts "someone's splitting with #{splitter.inspect}" }w.reverse#reversed!#=> "oof"w.split(//)#someone's splitting with //#=> ["f", "o", "o"]