[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: Serializable Proc

will

8/19/2008 10:30:00 PM

[Note: parts of this message were removed to make it a legal post.]

test


On 08/19/2008 "Patrick Li" <patrickli_2001@hotmail.com> wrote:




> You mean where the closure would point to the same variable after
> being reloaded? I'm not sure how that's going to work.

Yes. ie.
a = 3
p = lambda{a}
Marshal.dump(p, File.open("myProc.proc", "w")

so that I can go
p = Marshal.load(File.open("myProc.proc", "r"))
p[] #should print 3

The 'connections' between variables should be saved. As well as the
values of the variables currently in the closure.

--
Posted viahttp://www.ruby-....





1 Answer

James Coglan

8/20/2008 5:17:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

One possible stab at it (works on Ubutun, couldn't get it to work on
Windows):

#================================
require 'rubygems'
require 'ruby2ruby'

def serialize_block(&block)
return nil unless block_given?
klass = Class.new
klass.class_eval do
define_method :serializable, &block
end
str = RubyToRuby.translate(klass, :serializable)
str.sub(/^def \S+\(([^\)]*)\)/, 'lambda { |\1|').sub(/end$/, '}')
end

s = serialize_block do |*args|
something do
args.join(', ')
end
end

puts s
#================================

Outputs:

lambda { |*args|
something { args.join(", ") }
}

This won't sort out the closure business, but will at least extract the
source code of a Proc.