Serializable Proc

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

···

--
Posted via http://www.ruby-forum.com/.

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

···

On Aug 19, 2008, at 1:51 PM, Patrick Li 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 via http://www.ruby-forum.com/\.

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

···

2008/8/19 Patrick Li <patrickli_2001@hotmail.com>

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

test

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.

···

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

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

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.

···

--
Posted via http://www.ruby-forum.com/.

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.