新手问题 一直没弄懂的一个地方,String === String 恒返回 false?

qq2729877005 · 2019年10月10日 · 最后由 qq2729877005 回复于 2019年10月12日 · 2232 次阅读

如图,这个东西一直没弄明白,也尝试去看了下 === 和 == 的实现,但无奈,c 语言水平问题,没看明白,只能发帖求助了😂

看一下这篇文章吧,讲得比较全面 https://blog.arkency.com/the-equals-equals-equals-case-equality-operator-in-ruby/

Class / Module

mod === obj #→ true or false

=== returns true if obj is an instance of mod or one of mod’s descendants. Of limited use for modules, but can be used to classify objects by class.

Basically implemented as

obj.kind_of?(mod)

所以:

Module === Module # => true
Class === Class # => true
Object === Object # => true
String === String # => false

你想看对应的 C 实现,也要先找对文档嘛。String===String 实际调用的是 Module 中的===,不是 Object 的===

spike76 回复

懂了,原来是看错地方了,感谢老铁👍

qq2729877005 关闭了讨论。 10月12日 11:11
qq2729877005 关闭了讨论。 10月12日 11:11
需要 登录 后方可回复, 如果你还没有账号请 注册新账号