From: Robert Klemme Date: 2005-03-24T05:39:53+09:00 Subject: Re: Iterating through a string and removing leading characters "Randy Kramer" schrieb im Newsbeitrag news:200503231224.46149.rhkramer@gmail.com... > On Wednesday 23 March 2005 10:29 am, Florian Gross wrote: >> Randy Kramer wrote: >> > Aside: but: >> > >> > irb(main):005:0> 10.times { puts s1[2].id } >> > 211 [times ten] >> > => 10 >> > irb(main):006:0> >> > >> > Which (maybe) looks nicer/more reasonable, except I don't know why the >> > 3 >> > digit ID in this case. >> >> string[number] returns either nil or the ASCII number of the character >> at that place. > > Thanks, but I'm still confused--s1 is "This is a test", so 211 is neither > nil > nor ASCII for "i", and besides, I asked for the (object_)id. > > 211 does happen to be 2*?i+1--maybe there's a clue there? (and the same > thing > holds for the previous character (h) which shows up as 209) I think there is a relation between object ids and values for Fixnums but I'm not sure. It's also quite unimportant IMHO. But it seems to be exactly the relationship you assumed: >> 20.times {|ch| printf "%02x %02x %02x\n", ch, ch.id, (ch<<1)+1} 00 01 01 01 03 03 02 05 05 03 07 07 04 09 09 05 0b 0b 06 0d 0d 07 0f 0f 08 11 11 09 13 13 0a 15 15 0b 17 17 0c 19 19 0d 1b 1b 0e 1d 1d 0f 1f 1f 10 21 21 11 23 23 12 25 25 13 27 27 => 20 > My original concern: I was hoping that s1[0] and similar did not create > new > objects, and I suspect they don't, but I'm not sure. They don't because Fixnums are treated specially for performance reasons. >> s = "This is a test" => "This is a test" >> 10.times {c=s[2]; p [c, c.chr, c.id]} [105, "i", 211] [105, "i", 211] [105, "i", 211] [105, "i", 211] [105, "i", 211] [105, "i", 211] [105, "i", 211] [105, "i", 211] [105, "i", 211] [105, "i", 211] => 10 Regards robert