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)

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.

Randy Kramer

>
> So "foo"[0] == ?f. And the object_id of low Fixnums is usually very low
> as their value is directly stored in it.
>
> You can work around this by instead doing "foo"[0, 1] which will return
> "f". I think this behavior is subject to change in Rite, but it's not
> yet clear what it will change to.