Project

General

Profile

Actions

Misc #20509

open

Document importance of #to_ary and #to_hash for Array#== and Hash#==

Added by gettalong (Thomas Leitner) 6 months ago. Updated 4 months ago.

Status:
Open
Assignee:
-
[ruby-core:118012]

Description

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).]

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0