
Issue #20509 has been updated by Dan0042 (Daniel DeLorme).
Maybe we should document this expectation clearly in the reference.
Definitely, because this is all new and surprising to me even with 20+ years of ruby experience. ```ruby o = Object.new def o.to_ary [1,2,3] end [1,2,3] == o #=>false [1,2,3] == o.to_ary #=>true #I expected these two expressions to be equivalent ``` So when we define #to_ary we also should define #== It's the first time I hear about this. It's also rather inconvenient, and imho not worth the benefit of "reduce unnecessary object allocation". ---------------------------------------- Misc #20509: Document importance of #to_ary and #to_hash for Array#== and Hash#== https://bugs.ruby-lang.org/issues/20509#change-109329 * Author: gettalong (Thomas Leitner) * Status: Open ---------------------------------------- Both `Array#==` and `Hash#==` provide special behaviour in case the `other` argument is not an Array/Hash but defines the special `#to_ary`/`#to_hash` methods. Those methods are never called, they are just checked for existence. And if they exist, `other#==` is called to allow the `other` argument to decide whether the two objects are equal. I think this is worth mentioning in the documentation for `Array#==` and `Hash#==`. [Background: In my PDF library HexaPDF I have defined two classes `PDFArray` and `Dictionary` which act like Array and Hash but provide special PDF specific behaviour. For PDFArray I defined the `#to_ary` method but for Dictionary just the `#to_h` method. I have come across a bug where comparing Arrays with PDFArrays just works as it should be but comparing Hashes with Dictionaries doesn't due to the absence of `#to_hash` (it seems I removed `Dictionary#to_hash` in 2017 due to problems with automatic destructuring when passing a Dictionary as argument; from what I see that should be no problem anymore, so I will just add it back).] -- https://bugs.ruby-lang.org/