On Wednesday 23 March 2005 03:34 pm, Florian Gross wrote:
> Randy Kramer wrote:
> > 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)
>
> irb(main):001:0> ?i.id
> (irb):1: warning: Object#id will be deprecated; use Object#object_id
> => 211
>
> This is consistent with the explanation from my earlier posting:
>
> 'So "foo"[0] == ?f. And the object_id of low Fixnums is usually very low
> as their value is directly stored in it.'
>
> > 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.
>
> Fixnums are never created (they are only referred to) so this does not
> cause any object creation overhead.

Thanks, Florian and Robert!  (It took a little while for this to sink in, but 
I think I've now got it.  Hope I can remember it ;-)

Well, maybe I'd better recap for myself (correct me if I'm still off base):

   * (from earlier emails) Something like s1[start, length] (which I 
misunderstood earlier to be s1[start, end]) doesn't just let you look at a 
portion (substring) of the original string, but actually creates a new 
(sub)string (with all the overhead of creating a new string).

   * on the other hand, s1[index] does not create a new (one byte) string, but 
simply returns a Fixnum (which happens to be an object) representing the 
ASCII value of the character.  In other words, this has none of the overhead 
of creating a new string.  (I guess this is where I'm still a little 
uncertain--does it go through some of the overhead of creating a new string, 
but the resultant new (one byte) strings all have the same value (the ASCII 
code for "i" which is Fixnum 105 of which (see next item) there is only one, 
which has object_id 211?  Does it really matter to me?  Probably not, maybe 
it's just idle curiosity.)

   * when I do an object_id on that Fixnum, I always get the same value (211 
for 105 (the Fixnum which represents "i")) because there is only one Fixnum 
object in the system with the value 105.

I don't need a response to my "idle curiosity" question under the 2nd bullet 
if the rest of my understanding is basicly (sp?) correct.

regards,
Randy Kramer