Joel VanderWerf
7/28/2007 7:06:00 PM
Larry Fast wrote:
> It looks like YAML can't restore a Method pointer. True? Or am I doing
> something wrong here. Is this a known Ruby bug or a limitation of YAML
> or a Larry-is-a-goof problem? The following code frag fails on the last
> line when it tries to load the YAML string...
Methods can't be serialized, by Marshal or by YAML. A serialized method
would have to contain the source code of the method (or something
equivalent to the source, such as a parse tree).
Note that Object#method returns a Method object, which will not be
affected if you later redefine #myMethod in MyKlass (see below). So
maybe it is best not to think of it as a pointer or a symbolic
reference, but as a sort of snapshot of the method, including the
definition as well as the "self".
Is it possible for you to use the method name instead of capturing a
Method object?
class MyKlass
attr_reader :myMethodPtr
def initialize
@myMethodPtr = method(:myMethod)
end
def myMethod
puts "command"
end
end
myObj = MyKlass.new
myObj.myMethodPtr.call
class MyKlass
def myMethod
puts "FOO"
end
end
myObj.myMethodPtr.call
__END__
Output:
command
command
--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407