From: Ryan Davis Date: 2005-07-14T17:13:07+09:00 Subject: Re: [SOLUTION] SerializableProc (#38) Granted, we cheated, quite a bit at that, but I think the solution we came up with is pretty: require 'r2c_hacks' class ProcStore # We have to have this because yaml calls allocate on Proc def initialize(&proc) @p = proc.to_ruby end def call(*args) eval(@p).call(*args) end end code = ProcStore.new { |x| return x+1 } => # The latest release of ZenHacks added Proc.to_ruby among other things. Granted, it doesn't preserve the actual closure, just the code, but it looks like that is a limitation of the other solutions as well, so we aren't crying too much. Our original solution just patched Proc and added _load/_store on it, but it choked on the YAML serialization side of things. Not entirely sure why, and we were too tired to care at the time. To see what we do to implement Proc.to_ruby: class Proc ProcStoreTmp = Class.new unless defined? ProcStoreTmp def to_ruby ProcStoreTmp.send(:define_method, :myproc, self) m = ProcStoreTmp.new.method(:myproc) result = m.to_ruby.sub!(/def myproc\(([^\)]+)\)/, 'proc do |\1|') return result end end -- ryand-ruby@zenspider.com - Seattle.rb - http://www.zenspider.com/ seattle.rb http://blog.zenspider.com/ - http://rubyforge.org/projects/ruby2c