Project

General

Profile

Actions

Bug #1852

closed

Enumerable's #hash Raises ArgumentError When Recursive Values are Present

Added by runpaint (Run Paint Run Run) over 14 years ago. Updated almost 13 years ago.

Status:
Closed
Target version:
ruby -v:
ruby 1.9.2dev (2009-08-01 trunk 24343) [i686-linux]
Backport:
[ruby-core:24648]

Description

=begin
Enumerable's #hash methods now raise ArgumentErrors when the structure contains a recursive reference. This is hopefully a bug. #hash is expected to return a Fixnum, not make judgments about the object. #to_s and #inspect both work with such objects, which reinforces my assumption.

h={}
=> {}
h[:key] = h
=> {:key=>{....}}
h.hash
ArgumentError: recursive key for hash

a=[]
=> []
a << a
=> [[....]]
a.hash
ArgumentError: recursive key for hash

(Note that the exception message is wrong here, too).

This is presumably the same reason that methods like Hash#invert and Array#sort now raise under the same conditions. I suppose this could be intentional, but if so could this policy be clarified? The status quo seems to be that:

  • Recursive Hash keys are prohibited. An ArgumentError is raised if an attempt is made to create one.
  • Recursive values (for hashes and arrays) are allowed.
  • Some methods (I've yet to compile a list) will raise an ArgumentError when called on an object with recursive values.

This behavior doesn't seem desirable to me. I'd rather one or the other: Enumerable methods simply make the best of recursive structures (as before); or, recursive values are prohibited outright (as with Hash keys).
=end


Files

recursive_hash.diff (7.24 KB) recursive_hash.diff marcandre (Marc-Andre Lafortune), 09/13/2009 02:46 PM
hash_additional.diff (5.04 KB) hash_additional.diff additional changes to recursive_hash.diff marcandre (Marc-Andre Lafortune), 09/15/2009 06:59 AM
hash_merged.diff (10.2 KB) hash_merged.diff combined diff of additional changes and recursive_hash.diff marcandre (Marc-Andre Lafortune), 09/15/2009 06:59 AM
catch.diff (2.18 KB) catch.diff marcandre (Marc-Andre Lafortune), 09/16/2009 05:36 AM
hash_merged2.diff (9.04 KB) hash_merged2.diff marcandre (Marc-Andre Lafortune), 09/16/2009 05:36 AM
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0