什么 Monkeypatch 创建了私有方法 split?
问题 Ruby:调用 3:Fixnum 的私有方法 和 私有方法`split'调用nil:NilClass (NoMethodError) 分别提到 Fixnum
和 NilClass
对象的私有方法 split
。
这个私有 split
方法是猴子补丁的伪关键字(如 print
和 puts
)吗?如果是这样,是什么添加了它,为什么他们使用 String
已经存在的方法名称?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以自己解决这个问题(即使不阅读其他答案):
您可以从我的 Ruby Method Lookup Flow (PDF 版本)所有对象的方法都在实例方法处完成
对象
...它本身包括内核
。因此,Kernel
的所有实例方法(添加了许多可用作顶级便利方法的情况)也最终成为每个对象上的方法。请注意,在 1.9+ 中情况并非如此,因为
Kernel#split
已被删除:You can solve this yourself (even without reading the other answer):
You can see from my Ruby Method Lookup Flow (PDF version) that methods for all objects finish at the instance methods of
Object
…which itself includesKernel
. Thus, all instance methods ofKernel
(added many cases to be available as top-level convenience methods) also end up as methods on every object.Note that this is not true in 1.9+ as
Kernel#split
has been removed: