Henry Oss wrote:
>> The 'i' in your method is local to the method.  It is not the same 'i' 
>> as outside the method.
>> 
>> Instead, do
>> 
>>  def add(i,j)
>>    i = i + j
>>  end
>> 
>>    i = 1
>> 
>>    puts i
>>     i = add(i,3)
>>    puts i
> 
> Thanks for that, but I was looking for a way of passing by address.  In 
> fact I thought that, because everything was an object in Ruby everything 
> was always passed by address.

The number 1 is an object. The variable i is not an object; it is a 
placeholder which contains a reference to the object.

Object references are always passed by value. You can never get a 
pointer to the placeholder.

This means that foo(i) cannot affect the value of i, because inside the 
method it's using a copy of that reference. If i refers to a mutable 
object, then the object itself can alter its state, but the reference 
does not change.

def a(x, y)
  x << y
end

i = "hello"
a(i, "x")
puts i   # "hellox" - but it's the same string object

i = 3
a(i, 1)  # calculates and returns 3 << 1 (=6)
puts i   # but i is still the same Fixnum object (=3)
-- 
Posted via http://www.ruby-forum.com/.