From: Rick DeNatale Date: 2007-10-03T02:05:17+09:00 Subject: Re: a different type of reference (shocked) On 9/29/07, David A. Black wrote: > Hi -- > > On Sat, 29 Sep 2007, Phlip wrote: > > > SpringFlowers AutumnMoon wrote: > > > >> (and in Ruby, we call a method by "pass by value, the value being the > >> reference (pointer) to an object). and when the method returns > >> something, it returns a value, which is the reference to an object.) It > >> is very consistent all the way. In Ruby, we don't have the "alias > >> reference", right? > > > > In Ruby, types that can (generally) fit into 32 bits are "immediate > > types". These Fixnums and Floats are pass-by-value. All other types - > > from String up - are pass by reference: > > I'd describe it more as SpringFlowers did: pass by value, where the > value happens to be a reference. When you do this: > > s = "string" > do_something(s) > > you are passing s by its value, which is a reference to the string > object. Long ago I read a forgotten article which made the distinction between: pass by value pass by reference and pass by object reference Pass by value copies some state held in a variable, the subroutine can change that state without affecting the original variable. Pass by reference passes the address of the VARIABLE, and the subroutine can change the state held in the variable. Pass by object reference passes a reference to the object, you can't change the original variable, but you can change the object by invoking methods which mutate the object. In pure object-oriented languages like Ruby, pass by object reference is the rule. Assignment has similar semantics. Thinking of it this way has the advantage that we can treat object references a opaque. The fact that in a given implementation a reference to a particular object might also be the representation of it's state (i.e. immediate objects) becomes irrelevant. If we pass or assign a FixNum for instance, the fact that we can't change it is because Fixnums have no mutating methods, in fact it's a prerequisite that only objects with no mutating methods can have an immediate representation. And by the way, SpringFlower, Floats are NOT immediate objects in Ruby. -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.com/