From: Christopher Dicely Date: 2008-02-10T09:59:12+09:00 Subject: Re: Recursive array bug? On Feb 9, 2008 4:08 PM, Siep Korteling wrote: > Justin Collins wrote: > > Siep Korteling 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. > 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"