YAML problem with Method pointers

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 Fast

···

--
Posted via http://www.ruby-forum.com/.

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

···

--
       vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

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)

···

--
Posted via http://www.ruby-forum.com/.