From f4e0e8f781b05c767ad2472a43a4ed0727a75708 Mon Sep 17 00:00:00 2001 From: Lin Jen-Shin Date: Tue, 15 Feb 2011 22:25:18 +0800 Subject: [PATCH 1/2] psych/visitors/yaml_tree.rb: use public_method instead of method: Before this commit, we can't dump this object: Struct.new(:method).new('override') because it overrides :method method. Use public_method didn't really solve the problem, since now we can't dump objects override public_method. At any rate, respond_to? with one argument only checks for public methods, so here still might be better to use public_method in terms of consistency. I am not sure how to solve this perfectly, since any method could be overridden anyway. But this operation is only used to find the loc of defining file of to_yaml, and loc is only used to emit warnings, it might be OK to skip it if there's some error. A simple begin; rescue block rescuing StandardError might not be really bad. Next commit would apply this rescue block. --- ext/psych/lib/psych/visitors/yaml_tree.rb | 4 ++-- test/psych/visitors/test_yaml_tree.rb | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/ext/psych/lib/psych/visitors/yaml_tree.rb b/ext/psych/lib/psych/visitors/yaml_tree.rb index e7d182f..f7f11f8 100644 --- a/ext/psych/lib/psych/visitors/yaml_tree.rb +++ b/ext/psych/lib/psych/visitors/yaml_tree.rb @@ -76,7 +76,7 @@ module Psych end if target.respond_to?(:to_yaml) - loc = target.method(:to_yaml).source_location.first + loc = target.public_method(:to_yaml).source_location.first if loc !~ /(syck\/rubytypes.rb|psych\/core_ext.rb)/ unless target.respond_to?(:encode_with) if $VERBOSE @@ -297,7 +297,7 @@ module Psych # FIXME: remove this method once "to_yaml_properties" is removed def find_ivars target - loc = target.method(:to_yaml_properties).source_location.first + loc = target.public_method(:to_yaml_properties).source_location.first unless loc.start_with?(Psych::DEPRECATED) || loc.end_with?('rubytypes.rb') if $VERBOSE warn "#{loc}: to_yaml_properties is deprecated, please implement \"encode_with(coder)\"" diff --git a/test/psych/visitors/test_yaml_tree.rb b/test/psych/visitors/test_yaml_tree.rb index ed89e78..1cf0d63 100644 --- a/test/psych/visitors/test_yaml_tree.rb +++ b/test/psych/visitors/test_yaml_tree.rb @@ -38,6 +38,12 @@ module Psych assert_equal s.foo, obj.foo end + def test_override_method + s = Struct.new(:method).new('override') + obj = Psych.load(Psych.dump(s)) + assert_equal s.method, obj.method + end + def test_exception ex = Exception.new 'foo' loaded = Psych.load(Psych.dump(ex)) -- 1.7.4.1