Thursday, October 22, 2009

Ruby Proc Objects

In order to dynamically create methods in Ruby and then have those methods copied to a new object when it is duped, I stored the methods as instance variables of class Proc in the object and then did some pattern matching when duping the parent object to find and copy the Proc blocks. But after the object got duped, I got very unexpected behavior, as if the Proc objects that represent my dynamic methods didn't get duped. Behold:

"Proc objects are blocks of code that have been bound to a set of local variables."

Thus, duping (or cloning) a Proc object may give it a new object id, but it doesn't re-bind the variables. It'll still reference the same variables it referenced before. Good to know.

No comments: