如何在Ruby中封送lambda(Proc)? [英] How do I marshal a lambda (Proc) in Ruby?

查看:61
本文介绍了如何在Ruby中封送lambda(Proc)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Joe Van Dyk 询问了Ruby邮件列表:

Joe Van Dyk asked the Ruby mailing list:

在Ruby中,我想您无法封送lambda/proc对象,对吗?是 可能用Lisp或其他语言?

In Ruby, I guess you can't marshal a lambda/proc object, right? Is that possible in lisp or other languages?

我正在尝试做的事情:

l = lamda { ... }
Bj.submit "/path/to/ruby/program", :stdin => Marshal.dump(l)

因此,我正在向BackgroundJob发送一个lambda对象,其中包含 要执行的操作的上下文/代码.但是,猜测那是不可能的.一世 最终封送了包含说明的普通红宝石对象 程序运行后该怎么做.

So, I'm sending BackgroundJob a lambda object, which contains the context/code for what to do. But, guess that wasn't possible. I ended up marshaling a normal ruby object that contained instructions for what to do after the program ran.

推荐答案

您无法封送Lambda或Proc.这是因为它们都被视为闭包,这意味着它们会在定义它们的内存周围封闭并可以引用它. (要封送它们,您必须封送它们在创建时可以访问的所有内存.)

You cannot marshal a Lambda or Proc. This is because both of them are considered closures, which means they close around the memory on which they were defined and can reference it. (In order to marshal them you'd have to Marshal all of the memory they could access at the time they were created.)

尽管Gaius指出,您可以使用 ruby​​2ruby 来获得程序.也就是说,您可以封送代表红宝石代码的字符串,然后在以后重新评估它.

As Gaius pointed out though, you can use ruby2ruby to get a hold of the string of the program. That is, you can marshal the string that represents the ruby code and then reevaluate it later.

这篇关于如何在Ruby中封送lambda(Proc)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆