ruby - 为什么表达式“Object.singleton_class.instance_method(:refine)”会引发“NameError”?
问题描述
众所周知,该方法Module#refine
是Module
类中的私有实例方法:
% 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#send
BasicObject#__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>]
在上面的结果中,除了refine
method append_features
、和method raise之外prepend_features
,其他方法都成功了方法查找。extend_object
module_function
NameError
它看起来很好奇,我想知道会发生什么。
解决方案
经过检查,它看起来像是:refine
一个, UnboundMethod
但我是pry
通过四处寻找的。从撬控制台:
Module.private_instance_methods(:false)
你会注意到:
[ 76] refine(arg1) Module (unbound)
本文档应解释NameError
您的代码示例中提出的问题
推荐阅读
- rust - 如何在 NonNull 中添加/减去偏移量
? - pdf - 无法使用 Adobe Acrobat 5.0 PDFWriter、Windows 10、VB6 和 Crystal Reports 进行打印?
- python - 在Python中的csv文件中添加两个列表以分隔列
- javascript - Twig(或 Symfony?)不运行我的 jQuery 脚本
- mysql - ElasticSearch 6.2:来自 MySQL 的自动完成搜索
- sql - GROUP BY 子句或在聚合函数中使用
- c - C中的点符号?
- java - Oauth2 客户端注销不起作用
- android - Cordova:StatusBar 插件适用于模拟器,但不适用于设备
- kotlin - 这个 takeWhileInclusive 的实现安全吗?