# YAML problem with Method pointers

**URL:** <https://www.ruby-forum.com/t/yaml-problem-with-method-pointers/110647>\
**Category:** Ruby\
**Created:** [July 28, 2007, 8:48pm UTC](https://www.ruby-forum.com/t/yaml-problem-with-method-pointers/110647 "2007-07-28T20:48:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![lfast](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ee59a6/32.png) [@lfast](https://www.ruby-forum.com/u/lfast)\
**Post date:** [July 28, 2007, 8:48pm UTC](https://www.ruby-forum.com/t/yaml-problem-with-method-pointers/110647/1 "2007-07-28T20:48:47Z")

</div>

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…

class MyKlass  
def initialize  
@myMethodPtr = method(:myMethod)  
end  
def myMethod command  
end  
end

myObj = MyKlass.new

puts myObj.to\_yaml # YAML text generated but it’s missing the Method  
Info  
$newObj = YAML.load( myObj.to\_yaml ) # FAILS HERE

------ OUTPUT ------

— !ruby/object:MyKlass  
myMethodPtr: !ruby/object:Method {}

C:/ruby/lib/ruby/1.8/yaml.rb:133:in `transfer': allocator undefined for Method (TypeError) from C:/ruby/lib/ruby/1.8/yaml.rb:133:in`node\_import’  
from C:/ruby/lib/ruby/1.8/yaml.rb:133:in `load' from C:/ruby/lib/ruby/1.8/yaml.rb:133:in`load’  
from x\_yml\_builder.rb:12

Thanks,  
Larry F.

---

<div class="post-metadata">

**Author:** ![lfast](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ee59a6/32.png) [@lfast](https://www.ruby-forum.com/u/lfast)\
**Post date:** [July 28, 2007, 9:06pm UTC](https://www.ruby-forum.com/t/yaml-problem-with-method-pointers/110647/2 "2007-07-28T21:06:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![lfast](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ee59a6/32.png) [@lfast](https://www.ruby-forum.com/u/lfast)\
**Post date:** [July 29, 2007, 3:20am UTC](https://www.ruby-forum.com/t/yaml-problem-with-method-pointers/110647/3 "2007-07-29T03:20:14Z")

</div>

Thanks Joel,  
Since I’m using the method in other classes I switched to sending in the  
whole of myObj and then accessing the method. It’s not pretty but it  
gets the job done. The snippet below looks OK. I guess my code is only  
a mess because the method I want to use is part of a larger class. If I  
isolate the method I want the code will probably be cleaner.

Cheers,  
Larry

class MethodWrapper  
def myMethod  
puts “command”  
end  
end

class OtherKlass  
def initialize( methodWrapper )  
methodWrapper.myMethod  
end  
end

myObj = MethodWrapper.new

otherObj = OtherKlass.new(myObj)
