Serializable Proc

P

Patrick Li

Hi,
For my current project I need to be able to write an object to disk and
retrieve it later. However, the object contains references to Procs,
which cannot be serialized.

Are there any gems out there that let me serialize a Proc (with support
for closures)?

If there aren't, I'm prepared to write my own. Does anybody know enough
to point me in the right direction?

Thanks a lot
-Cuppo
 
J

James Gray

Are there any gems out there that let me serialize a Proc (with
support
for closures)?

You mean where the closure would point to the same variable after
being reloaded? I'm not sure how that's going to work.
If there aren't, I'm prepared to write my own. Does anybody know
enough
to point me in the right direction?

You may find some ideas in this old Ruby Quiz:

http://www.rubyquiz.com/quiz38.html

James Edward Gray II
 
P

Patrick Li

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.
 
J

James Coglan

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

2008/8/19 Patrick Li said:
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.



I was hoping ruby2ruby would help, but I can't get it to run on my Windows
box at present -- will try again on Ubuntu later. If anyone figures out how
to do this I would be thrilled, as I'm trying to do a similar thing in
JavaScript. Said language at least gives you a Function#toString method that
gets you part-way there, but getting the values of variables if the function
was created inside a closure is something I've not cracked yet.

James Coglan
http://blog.jcoglan.com
http://github.com/jcoglan
 
P

Patrick Li

Good to hear I'm not the only one with interest in this.

I'm also read into a gem called NodeWrap. I'm not sure if it's capable
of what I want, but I haven't been able to get it to install so far.
 
J

James Coglan

[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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,141
Messages
2,570,817
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top