首页 > 解决方案 > 为什么表达式“Object.singleton_class.instance_method(:refine)”会引发“NameError”?

问题描述

众所周知,该方法Module#refineModule类中的私有实例方法:

% Module.private_instance_methods(false)
[:remove_const, :method_added, :append_features, :method_removed, 
:method_undefined, :extend_object, :prepend_features, :using, :refine, 
:initialize, :initialize_copy, :initialize_clone, :public, :protected, 
:module_function, :private, :included, :extended, :prepended]

这意味着Object类本身可以使用or调用refine方法,因为是真的。Object#sendBasicObject#__send__Object.singleton_class.ancestors.include?(Module)

但是执行undefined method时会引发实际错误Object.refine

# ./tmp/mytest.rb
Module.private_instance_methods(false).map do |m|
  begin
    p [m, Object.singleton_class.instance_method(m)]
  rescue StandardError => e
    p [m, e.exception]
  end
end

输出是这样的。refine方法在中间。

% ruby -v
ruby 2.5.0p0 (2017-12-25 revision 61468) [x86_64-darwin17]
% ruby ./tmp/mytest.rb
[:remove_const, #<UnboundMethod: #<Class:Object>#remove_const>]
[:method_added, #<UnboundMethod: #<Class:Object>#method_added>]
[:append_features, #<NameError: undefined method `append_features' for class `#<Class:Object>'>]
[:method_removed, #<UnboundMethod: #<Class:Object>#method_removed>]
[:method_undefined, #<UnboundMethod: #<Class:Object>#method_undefined>]
[:extend_object, #<NameError: undefined method `extend_object' for class `#<Class:Object>'>]
[:prepend_features, #<NameError: undefined method `prepend_features' for class `#<Class:Object>'>]
[:using, #<UnboundMethod: #<Class:Object>#using>]
[:refine, #<NameError: undefined method `refine' for class `#<Class:Object>'>]
[:initialize, #<UnboundMethod: #<Class:Object>#initialize>]
[:initialize_copy, #<UnboundMethod: #<Class:Object>#initialize_copy>]
[:initialize_clone, #<UnboundMethod: #<Class:Object>#initialize_clone>]
[:public, #<UnboundMethod: #<Class:Object>#public>]
[:protected, #<UnboundMethod: #<Class:Object>#protected>]
[:module_function, #<NameError: undefined method `module_function' for class `#<Class:Object>'>]
[:private, #<UnboundMethod: #<Class:Object>#private>]
[:included, #<UnboundMethod: #<Class:Object>#included>]
[:extended, #<UnboundMethod: #<Class:Object>#extended>]
[:prepended, #<UnboundMethod: #<Class:Object>#prepended>]

在上面的结果中,除了refinemethod append_features、和method raise之外prepend_features,其他方法都成功了方法查找。extend_objectmodule_functionNameError

它看起来很好奇,我想知道会发生什么。

标签: ruby

解决方案


经过检查,它看起来像是:refine一个, UnboundMethod但我是pry通过四处寻找的。从撬控制台:

Module.private_instance_methods(:false)

你会注意到:

 [ 76]                     refine(arg1)                    Module (unbound)

文档应解释NameError您的代码示例中提出的问题


推荐阅读