From: Rick DeNatale Date: 2010-10-04T09:41:28+09:00 Subject: Re: Pass by reference and copy on write On Sun, Oct 3, 2010 at 1:49 PM, Caleb Clausen wrote: > > I like Rick's terminology: pass by object reference. I think that > makes it a little clearer what is really going on. Then if you > understand the subtleties of how Fixnums are actually packed into > their object references, you'll understand what is going on, and why > they behave differently than other classes. I thing that the value of thinking of this as call by object (reference) is that in languages like Smalltalk and Ruby the object references are opaque, and provide a barrier between the caller and the object's implementation. The only way for another object to get at any state on the other side of that barrier is by sending a message to the object, even if that message is instance_variable_get/set. The confusion about thinking of call by value, vs. call by reference in languages like Ruby is because sending a message to the object may change its state, something which if the state is visible via message sends will be observable via any reference to the object. So immutable objects and mutable objects can confuse someone 'coming from' C into thinking that one is passed by value, and the other by reference, rather than the uniform mechanism actually in use. The fact that some objects like Fixnums, nil, true, and false are immediate values and usually don't have state other than their identity is really an orthogonal thought, as is the ability in Ruby to add 'singleton' instance variables to even objects represented by immediate references. And object references play in more than just argument passing semantics. My dear friend from IBM, the late David N. Smith used to say when teaching Smalltalk that all objects in Smalltalk are the 'same size', and I would say that the same thing is true in Ruby. By this he didn't mean that all objects consumed the same amount of RAM for their state, but that the references were all the same memory size. So all instance variables are polymorphic object references, arrays are heterogeneous, and there's always room for a different breed of duck. Like Dr. Who's Tardis, objects are usually bigger on the inside than the outside. -- Rick DeNatale Blog: http://talklikeaduck.denhaven2.com/ Github: http://github.com/rubyredrick Twitter: @RickDeNatale WWR: http://www.workingwithrails.com/person/9021-rick-denatale LinkedIn: http://www.linkedin.com/in/rickdenatale