Hermann Martinelli
6/24/2007 5:00:00 PM
Starke wrote:
> As you can see, I pass in var1 into the method and then I change the
> value to 'test1'. I would like to method to go back and update var1,
> so that if passed into another method it reflects the change that
> method change var_in did. Does this make since? Im new to
> programming.
What you want to do is a "call by reference".
What you get is a "call by value".
This is just to let you know the terminology.
Search the web for more about these concepts.
As soon as you assign a new value (object) to
your local variable in the method change(), it
will no longer point to the object passed as
argument, but instead point to a new object.
The object that was passed remains unchanged.
The code below my signature could make a lot
of things clearer. Just run it and come back
if you have questions.
Btw: You don't need to know all this to use
Ruby. So don't worry and don't forget to have
fun with this wonderful and easy programming
language.
HM
def change x
puts "With value just passed:",
"ID of x: #{x.object_id}",
"Value of x: #{x}"
x = 'abc'
puts "With new value: ",
"ID of x: #{x.object_id}",
"Value of x: #{x}"
return x
end
a = "Hallo"
puts "Before calling change():",
"ID of a: #{a.object_id}"
rv = change a # return value
puts "After calling change():",
"ID of rv: #{rv.object_id}",
"Value of rv: #{rv}",
"ID of a: #{a.object_id}",
"Value of a: #{a}"