From: Emmanuel Oga Date: 2008-08-27T00:12:48+09:00 Subject: Re: Passing a block with define_method > I'm actually wrapping existing methods so I need to use define_method to > retain access to the old method. Here's my (broken) code so far: I think I saw this "trick" in one of Ara's posts :D * Store the method somewhere so it is not GC by ruby * Retrieve the method object from its object_id with ObjectSpace._id2ref * use eval instead of define_method > module Continuations NOGC = [] > def accepts_continuation(*args) > args.each do |method_name| > func = instance_method(method_name) NOGC << func # Stop garbage collection of the method. func_object_id = func.object_id > #define_method(method_name) do |*params, &block| > # value = func.bind(self).call(*params) > #end eval(<<-CODE, __FILE__, __LINE__) def #{ method_name }(*params, &block) func = ObjectSpace._id2ref(#{ func_object_id }) func.bind(self).call(*params, &block) end CODE > end > end > end -- Posted via http://www.ruby-forum.com/.