Bug #1852: Enumerable's #hash Raises ArgumentError When Recursive Values are Present
http://redmine.ruby-lang.org/issues/show/1852

Author: Run Paint Run Run
Status: Open, Priority: Normal
Category: core
ruby -v: ruby 1.9.2dev (2009-08-01 trunk 24343) [i686-linux]

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


----------------------------------------
http://redmine.ruby-lang.org