# Bug filed: Array#hash should properly handle recursive arrays

**URL:** <https://www.ruby-forum.com/t/bug-filed-array-hash-should-properly-handle-recursive-arrays/165742>\
**Category:** IronRuby\
**Created:** [April 16, 2009, 11:22pm UTC](https://www.ruby-forum.com/t/bug-filed-array-hash-should-properly-handle-recursive-arrays/165742 "2009-04-16T23:22:53Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![murena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7ba0ec/32.png) [@murena](https://www.ruby-forum.com/u/murena)\
**Post date:** [April 16, 2009, 11:22pm UTC](https://www.ruby-forum.com/t/bug-filed-array-hash-should-properly-handle-recursive-arrays/165742/1 "2009-04-16T23:22:53Z")

</div>

Hi,

I have just filed this bug on CodePlex:  
[http://ironruby.codeplex.com/WorkItem/View.aspx?WorkItemId=605](http://ironruby.codeplex.com/WorkItem/View.aspx?WorkItemId=605)

It is actually pretty easy to fix with a bunch of lines of code in  
RubyArray.GetHashCode (just tested, too bad IronRuby.dll is not open  
to contributions) and, as a result, this would resolve the following 4  
critical failures for the array core specs:

Array#hash properly handles recursive arrays  
Array#| properly handles recursive arrays  
Array#uniq properly handles recursive arrays  
Array#uniq! properly handles recursive arrays

Regards

–  
Daniele A.  
[http://www.clorophilla.net/blog/](http://www.clorophilla.net/blog/)  
[http://twitter.com/JoL1hAHN](http://twitter.com/JoL1hAHN)

---

<div class="post-metadata">

**Author:** ![murena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7ba0ec/32.png) [@murena](https://www.ruby-forum.com/u/murena)\
**Post date:** [April 17, 2009, 12:09am UTC](https://www.ruby-forum.com/t/bug-filed-array-hash-should-properly-handle-recursive-arrays/165742/2 "2009-04-17T00:09:07Z")

</div>

I can fix RubyArray.GetHashCode to handle the simple case of an array  
containing itself.

However, the simple fix wont deal with the case where the array is  
indirectly recursive as in this snippet.  
a = []  
b = [a]  
a\<\<b

---

<div class="post-metadata">

**Author:** ![murena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7ba0ec/32.png) [@murena](https://www.ruby-forum.com/u/murena)\
**Post date:** [April 17, 2009, 12:24am UTC](https://www.ruby-forum.com/t/bug-filed-array-hash-should-properly-handle-recursive-arrays/165742/3 "2009-04-17T00:24:56Z")

</div>

Curt pointed out a way to fix the recursive case using  
RubyUtils.RecursionTracker…

---

<div class="post-metadata">

**Author:** ![murena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7ba0ec/32.png) [@murena](https://www.ruby-forum.com/u/murena)\
**Post date:** [April 17, 2009, 12:30am UTC](https://www.ruby-forum.com/t/bug-filed-array-hash-should-properly-handle-recursive-arrays/165742/4 "2009-04-17T00:30:12Z")

</div>

Great, that’s exactly what I used 🙂

Thanks

On Fri, Apr 17, 2009 at 00:22, Shri B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> However, the simple fix wont deal with the case where the array is indirectly recursive as in this snippet.  
> Hi,  
> Array#| properly handles recursive arrays  
> Ironruby-core mailing list  
> removed\_email\_address@domain.invalid  
> [http://rubyforge.org/mailman/listinfo/ironruby-core](http://rubyforge.org/mailman/listinfo/ironruby-core)

–  
Daniele A.  
[http://www.clorophilla.net/blog/](http://www.clorophilla.net/blog/)  
[http://twitter.com/JoL1hAHN](http://twitter.com/JoL1hAHN)

---

<div class="post-metadata">

**Author:** ![murena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7ba0ec/32.png) [@murena](https://www.ruby-forum.com/u/murena)\
**Post date:** [April 17, 2009, 1:15am UTC](https://www.ruby-forum.com/t/bug-filed-array-hash-should-properly-handle-recursive-arrays/165742/5 "2009-04-17T01:15:14Z")

</div>

Does Ruby call “hash” dynamically on each element of the array?  
If not then there is no need for RecursionTracker. A simple  
non-recursive DFS with a dictionary that remembers visited objects  
should do.

Tomas

---

<div class="post-metadata">

**Author:** ![murena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7ba0ec/32.png) [@murena](https://www.ruby-forum.com/u/murena)\
**Post date:** [April 17, 2009, 1:16am UTC](https://www.ruby-forum.com/t/bug-filed-array-hash-should-properly-handle-recursive-arrays/165742/6 "2009-04-17T01:16:24Z")

</div>

See RubyFileOps.Join for an example.

Tomas

---

<div class="post-metadata">

**Author:** ![murena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7ba0ec/32.png) [@murena](https://www.ruby-forum.com/u/murena)\
**Post date:** [April 17, 2009, 4:01am UTC](https://www.ruby-forum.com/t/bug-filed-array-hash-should-properly-handle-recursive-arrays/165742/7 "2009-04-17T04:01:07Z")

</div>

The simple test I did suggested that MRI dynamically calls hash on each  
element of the array.
