Recursive array bug?

S

Siep Korteling

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
 
J

Justin Collins

Siep said:
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


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
 
S

Siep Korteling

Justin said:
Siep said:
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
 
C

Christopher Dicely

Justin said:
Siep said:
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.

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"
 
R

Robert Klemme

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,283
Messages
2,571,405
Members
48,098
Latest member
inno vation

Latest Threads

Top