From: Rick DeNatale Date: 2007-10-05T14:37:05+09:00 Subject: Re: Reference vs. Reference On 10/5/07, Eric Mahurin wrote: > There are a whole slew of languages that pass arguments like ruby. > Most seem to call it call-by-value where the value is an object > handle/pointer/reference. Python guys call it > call-by-object-reference. Not just python guys. The term predates python by quite a bit. I'm pretty sure it was used in the Smaltalk literature, and a recent google search showed it in papers on Emerald, and distributed OO language from the 1980s. > Like Java, Ruby does have the equivalent of "intrinsics". They are > the immediates (Fixnum, TrueClass, FalseClass, NilClass, and Symbol) > which when used behave like the more conventional call-by-value. Part of the problem with this whole discussion is that it's crossing meta-levels. Immediates/intrinsics exist in the implementation, below the language level. To the Java/Smalltalk/Ruby programmer the encoding of object references is invisible. Object references are object references. The only relevant difference between say a Fixnum and an Array is that all instances of Fixnum are immutable. Since there are no methods on 1 which can mutate it to 2 or any other object a method which gets it as an argument can't change it. > If you really wanted to get the semantics of call-by-reference in > ruby, you'd have to do something like the following. > > def foo(get_bar, set_bar) > get_bar[] # get the value of the bar reference : 123 > set_bar[345] # set the value of the bar reference > get_bar[] # 345 > end > > bar = 123 > foo(lambda {bar}, lambda {|v| bar=v}) > > Of course you could combine the getter/setter into one lambda (with an > optional setting value) and/or wrap this into an object to make it > look prettier. This is more like call-by-name with the lambda(s) acting as a thunk. -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.com/