Ruby是否有== AND eql的任何充分理由? ? (与to_a和to_ary类似) [英] Any good reason for Ruby to have == AND eql? ? (similarly with to_a and to_ary)

查看:58
本文介绍了Ruby是否有== AND eql的任何充分理由? ? (与to_a和to_ary类似)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道eql吗?由哈希用于检查对象是否与键*匹配,您可以这样做

I know eql? is used by Hashes to see if an object matches a key*, and you do

def ==(rb)

如果要支持==运算符,但一定有充分的理由说哈希表不使用==.这是为什么?您什么时候会有==和eql的定义? 等价(例如,一个是另一个的别名)?

if you want to support the == operator, but there must be a good reason that Hashes don't use == instead. Why is that? When are you going to have definitions for == and eql? that are not equivalent (e.g. one is an alias to the other) ?

类似地,为什么除了to_a之外还有to_ary?

Similarly, why have to_ary in addition to to_a?

此问题是针对另一个问题.

*当然,哈希也假设eql吗? == true表示散列码相等.

* Of course, a Hash also assumes eql? == true implies that the hashes codes are equal.

此外,覆盖平等基本上是一个可怕的想法吗? ?

Also, is it basically a terribly idea to override equal? ?

推荐答案

我不知道在红宝石中做出这种特定选择的理由,但是我只想指出,平等是一个困难的概念.

I don't know the reasoning for this particular choice in ruby, but I'll just point out that equality is a difficult concept.

Common Lisp,例如具有eq,eql,equal,equalp,就此而言=

Common Lisp, for example has eq, eql, equal, equalp, and for that matter =

能够区分两个引用相同的对象,两个具有相同值的相同类型的不同对象,两个具有相同值但类型不同的对象等的区别非常有用.变体有意义取决于语言中什么才有意义.

It can be very useful to be able to tell the difference between two references to the same object, two different objects of the same type with the same value, two objects with the same value but of different types, etc. How many variations make sense depends on what makes sense in the language.

如果我没记错的话(我不使用ruby),则rubys谓词将实现其中三种情况

If I recall it correctly (I don't use ruby), rubys predicates are implementing three of these cases

==是值相等

eql?是值和类型相等

eql? is equality of value and type

相等吗?仅对同一对象为真

equal? is true only for the same object

这篇关于Ruby是否有== AND eql的任何充分理由? ? (与to_a和to_ary类似)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆