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

qq2729877005 · October 10, 2019 · Last by qq2729877005 replied at October 12, 2019 · 2232 hits

如图,这个东西一直没弄明白,也尝试去看了下 === 和 == 的实现,但无奈,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 的===

Reply to spike76

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

qq2729877005 closed this topic. 12 Oct 11:11
qq2729877005 closed this topic. 12 Oct 11:11
You need to Sign in before reply, if you don't have an account, please Sign up first.