Recursive array bug?

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

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

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

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

On Feb 9, 2008 4:08 PM, Siep K. [email protected] 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”