Ruby ==, eql? , === 核心类 Override 一览表

mingyuan0715 · May 05, 2018 · Last by teddy_1004 replied at May 07, 2018 · 1438 hits

基本知识可参考社区多篇文章:https://ruby-china.org/topics/20334

  • 只检查类本身是否有 Override,有些类的父类做了 Override 了的;
  • Module/Symbol虽然有Override ==方法,但是代码跟 BasicObject 的一样,可认为没有 Override;
  • Range 中的 ===并不完全等于 include?, 参数处理上略有差异;
Class/Module Override == Override eql? (Same as ==) Override === (Same as ==)
Array √(×) ×
Hash √(√) ×
Range √(×) √(include?
Comparable × ×
Exception × ×
Numeric × x
Complex × ×
Float √(×) √(√)
Integer × √(√)
Rational × ×
MatchData √(√) ×
Method √(√) √(call
Proc x x √(call
Module ×
Random × ×
Regexp √(√) √(×)
String √(×) √(√)
Struct √(×) ×
Symbol x x x
FalseClass x x
TrueClass x x
NilClass x x

纯体力活,每一个我都点开 c 实现源码看了。很多文章说 eql可认为是 == 的别名,大部分类有差异的。

申请+精

Reply to lithium4010

其实这个文章应该更深入一点,可以详细阐述下 每个核心类的 == 使用和 与 其他相等方法的差异。

申请+精

You need to Sign in before reply, if you don't have an account, please Sign up first.