On Dec 21, 12:26 ¨Âí¬ Òïâåòô Ëìåííå ¼óèïòôãõô®®®Àçïïçìåíáéì®ãïí¾ ÷òïôåº
> 2009/12/21 RichardOnRails <RichardDummyMailbox58... / uscomputergurus.com>:
>
> > I've made the following observations about Ruby's apparent
> > implementation:
>
> > 1. Integer's in the range -2**30 to 2**30-1 are Fixnum's. Integers
> > outside that range are Bignum's
> > 2. For Fixnum "i",  ¨Â§ó ïâêåãôßééó ô÷éãé§öáìõðìõó ±®  ¨Âáé> > another way, i's object_id is i''s binary value left-shifted with 1 as
> > padding.
> > 3. Object_id's of Bignum's close to the boundaries of Fixnum's bear no
> > apparent relationship to the Fixnum object_id's.
>
> All correct.
>
> > These observations suggest to me that an assignment statement like
> > "a=1" requires the interpreter to do no more than:
> > 1.  ¨Âåáòãæïôèéäåîôéæéåò ¢áéî ôèãõòòåîóãïðå®
> > 2.  ¨Âæïõîä¬ õóéôéæ îïô¬ åîôåéô éî ôèãõòòåîóãïðáîôèåî
> > use it.
> > 3. "Use it" means take the binary representation of the assigned
> > value,  ¨Âåæóèéæéô ÷éôè ¢±ðáääéîç¬ áîíáëå ôèáô ãõòòåî> > object_id of the identifier "a".
> > 4. There is no need to locate free space in an memory pool where
> > object values for Bignums, Strings and other object are stored.  ¨Âïò
> > Fixnums, the object_id IS the value, just a little shifted.
>
> > Is my speculation about this aspect of Ruby's implementation correct,
> > or am I all wet?
>
> Hm...  ¨Âèéìå ùïèáöå íáîù ôèéîçó òéçèô¬ äïî§æõììáçòåå®  ¨Â> variable (be it a local variable like "a" in your example or an
> instance variable like "@a") has an object id.  ¨Ââêåãéä éó > property of an object which might be referenced by many variables.
> Your step 3 actually mixes two separate things: evaluation of the
> expression of the right side of "=" and assignment. Using" in an
> assignments means to take whatever the expression spits out and store
> it in the variable.
>
> Now, what your right hand expression yields is an object reference.
> For optimization purposes some object references are special in that
> they actually *are* the object (these are the "immediate values" which
> Gary mentioned, Fixnums for example).  ¨Âèéäïåó îïíáëå óðåãéá> treatment for assignments necessary.  ¨Âáôèåò¬ éô íáëåóðåãéá> treatment for _method calls_ necessary.  ¨Âåãáõóå ôèåî ôèéîôåòðòåôå> does not have to look up the object on the heap etc.
>
> Someone with more intimate knowledge of the implementation might be
> able to explain this better.  ¨Âõô âåìéåöéô§ó éíðïòôáîôï ðïéî> out that the difference is not in the assignment but in the right hand
> side expression which - in the case of a Fixnum - yields a special
> object reference.
>
> Kind regards
>
> robert
>
> --
> remember.guy do |as, often| as.you_can - without endhttp://blog.rubybestpractices.com/

Hi Robert,

> No variable ... has an object id.

Robert,  I know, by virtue of reading a number of your responses in
this NG, that youÃÓe a Ruby expert.  But the statement referenced
above is contradicted by at least two authorities on Ruby:
1. .. every object has a unique object identifier (abbreviated as
object ID)[űrogramming Ruby  Second Edition, Thomas, et al, p.
12] , where you praised the bookÃÔ usefulness :-).   Of course, the
ÅÊdmethod has since be deprecated to ÅÐbject_idwhen we want to
reference an object ID.
2.  .. an object in Ruby has an identity: ÅÂbcÇÐbject_id
#53744407
This object ID is of limited utility." [ŵhe Ruby Way Second
Edition, Fulton, p.26]

> step 3 actually mixes two separate things: evaluation of the expression of the right side of "=" and assignment

Granted, I stuck to what I viewed as the essential issues.  Perhaps a
more complete estimate might read.

3.1	Ruby interprets ÅÂ = 1to mean ÅÂ.=(1) i.e.  invokes the93=method on the object.  Normally, that reference will cascade up to
Object.=, I suppose.
3.1a  If ÅÂis undefined in the current scope, ÅÂwill be added to
the scope with an ID of (2a+1) = 3.
3.1b Otherwise:
3.1.b.1 If aÃÔ ID is in the range of Fixnums, (or aÃÔ ID indicates
itÃÔ true, false, etc .), aÃÔ ID will be set to 3
3.1b.2 Otherwise, if aÃÔ ID indicates any other type, aÃÔ ID will be
pushed to the garbage stack and aÃÔ ID will be set to 3
Is that better?

> For optimization purposes some object references are special in that theyctually *are* the object

That sounds like you mean in the case of Fixnum   that the number
1 IS the ID as well as the value of the object.  But we can see ÅÑuts
1.object_id=> 3
So I think that fact (plus all the other evidence I offered) suggests
that IDÃÔ for FixnumÃÔ are manufactured as 2*value+1
QED???

As always, thanks for your insightful response.
Richard