@chris fullmer said:
I just tested by displaying the object id of a variable passed between different methods, and it keeps the same object id. So it sounds like it passes a reference.
`
def m2(a)
> puts a.id
> end
>
> def m1
> a = "hi"
> puts a.id
> m2(a)
> end
`Correction (line 8): "m2(a)" was "ms(a)"
What I think ya'll are misunderstanding (and contrary to what most programming tutorials say,) about Ruby, is that Ruby DOES NOT really have TRUE VARIABLES.
'True variables', I mean in the sense that other languages such as the old BASIC or Pascal had variables, that were declared to 'BE' a certain datatype (int, real, string, etc.) AND kept a table in memory of those (variable) identifiers along with the current VALUE that was 'HELD' by them.
Ruby doesn't do this. EVERYTHING in Ruby is an OBJECT, including objects of the BASE classes (ie: Integer, Float, String, etc.) So when you code:
aVar = 8
in Ruby, you are NOT assigning the integer value 8 to a variable identifier, ... you are instead creating a reference, with the name aVar that POINTS at the first class object 8. The object 8 is an instance of class Integer, and so inherits methods from it's class, and superclasses, ie:
` 8.id
an id number
aVar.id
the same id number since it points at object 8
bVar = aVar
bVar.id
the same id number since it points at object 8`
And as Chris said, Fixnum is special in that there can be ONLY 1 object for each ordinal in the Fixnum set.
The same is not truefor other more complex types, such as Array, String, etc.
The best illustration of "how thinking" of Ruby references as variables (like in other languages,) can lead you to 'hair-pulling' frustration, was given in the forum over at Google Groups. (I answered and solved the poster's problem, but refered in my answer, to his 'reference' as 'an alias'; perhaps not so good as alias is a special kind of reference in Ruby for methods and don't work for "variables".)
Any how.. read that post, and examine flesk's code, and the solution TIG and I gave him (TIG and I are about 5/6 hours apart and posted about the same time, I think.)
Google Groups (Sketchup ruby API): Variable value error
So how would you 'pass by value'? (In the sense that you would in Pascal. You should have an idea, after reading that post.)
Let's modify Chris' code a bit:`
def m2(a)
puts 'a in m2 has id; '+a.id.to_s
end
def m1
a = "hi"
puts 'a in m1 has id; '+a.id.to_s
m2(a.dup)
end
Or if you wish to paste into the console, then (1 line at a time): def m2(a); puts 'a in m2 has id: '+a.id.to_s; end def m1; a = "hi"; puts 'a in m1 has id: '+a.id.to_s; m2(a.dup); endand then type: m1`
You'll see the id numbers differ.
NOTE: If you think this will save time or memory, it won't. Ruby will create an anonymous reference to the duplicated object, so there is still a 'reference assignment' behind the scenes. In fact if you look up at the modified code, at the 2 puts statements, the String arguments 'a in m1 has id: ' are also transparently assigned an anonymous reference, so I might just as well have stored the string in a named reference of my own, so both methods could use the same String object, but change the method name. Of course it's not necessary, as these objects will not 'live' long.