Super simple newbie Q about methods

If I write:

def add(i,j)
i = i + j
end

i = 1

puts i
add(i,3)
puts i

I get

1
1

What would I have to change to get

1
4

? I presume this is covered in the Pickaxe book but can’t find the
answer - a pointer to whereabouts it is covered would be great.

Sorry to be so dumb.

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

Alex DeCaria 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. I would like to understand when it is not
the case.

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. I would like to understand when it is not
the case.

Then you need to use instance variables such as ‘@i’, or class variables
such as ‘@@i’. Ruby doesn’t support the use of pointers (as far as I
know at least).

–Alex

Henry O. wrote:

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. I would like to understand when it is not
the case.

Well…

def i, j
i[0] = i[0] + j
end

You will notice that the previous method requires i to be an Array.

i = [1]
puts i
add i, 2
puts i

Happy? :slight_smile:

Henry O. 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)

Hello,

Then you need to use instance variables such as ‘@i’, or class variables
such as ‘@@i’. Ruby doesn’t support the use of pointers (as far as I
know at least).

I would say rather on the contrary. Everything is a pointer but you
can reassign a variable in a new pointing direction without affecting
the previously pointed object. See

def add(a,b)
a.replace(a+b)
end
=> nil

s = “string”
=> “string”

add(s,"_added")
=> “string_added”

s
=> “string_added”

Here, the add method does not reassign variable a but send a message
to the object where a is pointing, saying that it should change it’s
value.
Of course, that would not work for a Fixnum, as those are immutable in
ruby.

Cheers,