Serializing a Proc doesn't seem to work

In Agile Web D. with Rails, there is a section on serializing
Ruby objects.

“We can assign any Ruby object to the last_five column” … p312

When I attempt to do this with a proc object, something like this is
saved in the db

— !ruby/object:Proc {}

Obviously, this doesn’t do much, and I attempt to use it …

NoMethodError: undefined method `call’ for “— !ruby/object:Proc {}\n
\n”:String
from (irb):5

Is it possible to save Proc objects in the db?

I am implementing a custom field scheme, where users can create custom
fields. I have Field Types, and I was thinking about using a ruby proc
to convert field entries to the correct object type.

John

Astorian wrote:

In Agile Web D. with Rails, there is a section on serializing
Ruby objects.

“We can assign any Ruby object to the last_five column” … p312

When I attempt to do this with a proc object, something like this is
saved in the db

— !ruby/object:Proc {}

Obviously, this doesn’t do much, and I attempt to use it …

NoMethodError: undefined method `call’ for “— !ruby/object:Proc {}\n
\n”:String
from (irb):5

Is it possible to save Proc objects in the db?

I am implementing a custom field scheme, where users can create custom
fields. I have Field Types, and I was thinking about using a ruby proc
to convert field entries to the correct object type.

“any” must be taken with a grain of salt. Only objects that have a
serializable state can be saved that way. You couldn’t serialize a
thread, for instance, or in your case, a proc.

You can use Ruby2Ruby to get the source for a proc, then you can eval it
when you deserialize it to get an equivalent proc back. It may be easier
for you to store some other information in the db to do your custom
field converstion. What you are proposing sounds pretty unwieldy to me.


Josh S.
http://blog.hasmanythrough.com