# Recursive array bug?

**URL:** <https://www.ruby-forum.com/t/recursive-array-bug/128656>\
**Category:** Ruby\
**Created:** [February 9, 2008, 11:35pm UTC](https://www.ruby-forum.com/t/recursive-array-bug/128656 "2008-02-09T23:35:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![steenslag](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c57346/32.png) [@steenslag](https://www.ruby-forum.com/u/steenslag)\
**Post date:** [February 9, 2008, 11:35pm UTC](https://www.ruby-forum.com/t/recursive-array-bug/128656/1 "2008-02-09T23:35:53Z")

</div>

I’ve just reinvented the recursive array.

a = ["a rose is "]  
a \<\< a

Expecting the end of the world, or at least my ruby session, I did:

a.flatten!

But flatten! was just waiting for idiots like me and returned a polite  
error. Investigating my unflattened array I found something strange.

p a[1][1][1][0]  
=\>a rose is

p a[0]  
=\> a rose is

p a[0][0]  
=\>97  
Huh? I expected nil.

p a[0][0][0]  
=\>1  
and it keeps returning 1’s as far as I can see. Is this a bug or am I  
just being silly?

Regards,

Siep

---

<div class="post-metadata">

**Author:** ![steenslag](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c57346/32.png) [@steenslag](https://www.ruby-forum.com/u/steenslag)\
**Post date:** [February 9, 2008, 11:56pm UTC](https://www.ruby-forum.com/t/recursive-array-bug/128656/2 "2008-02-09T23:56:26Z")

</div>

Siep K. wrote:

> error. Investigating my unflattened array I found something strange.
> 
> p a[0][0][0]  
> =\>1  
> and it keeps returning 1’s as far as I can see. Is this a bug or am I  
> just being silly?
> 
> Regards,
> 
> Siep

Does this help?

irb(main):001:0\> a = [“array”]  
=\> [“array”]  
irb(main):002:0\> a \<\< a  
=\> [“array”, […]]  
irb(main):003:0\> a[0] # This is Array#[]  
=\> “array”  
irb(main):004:0\> a[0][0] # This is String#[]  
=\> 97  
irb(main):005:0\> a[0][0][0] # This is Fixnum#[]  
=\> 1  
irb(main):006:0\> a[0][0][0][0][0] # This is also Fixnum#[]  
=\> 1

-Justin

---

<div class="post-metadata">

**Author:** ![steenslag](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c57346/32.png) [@steenslag](https://www.ruby-forum.com/u/steenslag)\
**Post date:** [February 10, 2008, 1:08am UTC](https://www.ruby-forum.com/t/recursive-array-bug/128656/3 "2008-02-10T01:08:59Z")

</div>

Justin C. wrote:

> Siep K. wrote:
> 
> > error. Investigating my unflattened array I found something strange.
> > 
> > p a[0][0][0]  
> > =\>1  
> > and it keeps returning 1’s as far as I can see. Is this a bug or am I  
> > just being silly?
> > 
> > Regards,
> > 
> > Siep
> 
> Does this help?
> 
> irb(main):001:0\> a = [“array”]  
> =\> [“array”]  
> irb(main):002:0\> a \<\< a  
> =\> [“array”, […]]  
> irb(main):003:0\> a[0] # This is Array#[]  
> =\> “array”  
> irb(main):004:0\> a[0][0] # This is String#[]  
> =\> 97  
> irb(main):005:0\> a[0][0][0] # This is Fixnum#[]  
> =\> 1  
> irb(main):006:0\> a[0][0][0][0][0] # This is also Fixnum#[]  
> =\> 1
> 
> -Justin

Yes it does. I had to think real hard about it. With a[0][0] I expected  
to get a value from a matrix (but there is no matrix, give me the blue  
pill please), when I’m actually calling a method from a string. Why it  
returns 97 is beyond me at this moment, but it’s not relevant. Duck  
typing fooled me, I guess.

Regards and thank you,

Siep

---

<div class="post-metadata">

**Author:** ![steenslag](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c57346/32.png) [@steenslag](https://www.ruby-forum.com/u/steenslag)\
**Post date:** [February 10, 2008, 2:10am UTC](https://www.ruby-forum.com/t/recursive-array-bug/128656/4 "2008-02-10T02:10:50Z")

</div>

On 10.02.2008 01:08, Siep K. wrote:

> Yes it does. I had to think real hard about it. With a[0][0] I expected  
> to get a value from a matrix (but there is no matrix, give me the blue  
> pill please), when I’m actually calling a method from a string. Why it  
> returns 97 is beyond me at this moment, but it’s not relevant. Duck

irb(main):001:0\> “a”[0]  
=\> 97  
irb(main):002:0\> 97.chr  
=\> “a”  
irb(main):003:0\> ?a  
=\> 97

Cheers

robert

---

<div class="post-metadata">

**Author:** ![steenslag](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c57346/32.png) [@steenslag](https://www.ruby-forum.com/u/steenslag)\
**Post date:** [February 10, 2008, 2:00am UTC](https://www.ruby-forum.com/t/recursive-array-bug/128656/5 "2008-02-10T02:00:17Z")

</div>

On Feb 9, 2008 4:08 PM, Siep K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > 
> > 
> > irb(main):003:0\> a[0] # This is Array#[]  
> > Yes it does. I had to think real hard about it. With a[0][0] I expected  
> > to get a value from a matrix (but there is no matrix, give me the blue  
> > pill please), when I’m actually calling a method from a string. Why it  
> > returns 97 is beyond me at this moment, but it’s not relevant. Duck  
> > typing fooled me, I guess.

It gets 97 because “a rose is”[0] is 97; that is, 97 is the ASCII  
character code of “a”:

irb(main):006:0\> text = “a rose is”  
=\> “a rose is”  
irb(main):007:0\> arr = (0…(text.length-1)).map {|idx| text[idx]}  
=\> [97, 32, 114, 111, 115, 101, 32, 105, 115]  
irb(main):008:0\> new\_text = arr.inject(“”) {|str, code| str + code.chr}  
=\> “a rose is”
