Forum: Ruby YAML problem with Method pointers

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Larry F. (Guest)
on 2007-07-28 22:48
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.
Joel VanderWerf (Guest)
on 2007-07-28 23:06
(Received via mailing list)
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
Larry F. (Guest)
on 2007-07-29 05:20
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)
This topic is locked and can not be replied to.