# Super simple newbie Q about methods

**URL:** <https://www.ruby-forum.com/t/super-simple-newbie-q-about-methods/188428>\
**Category:** Ruby\
**Created:** [May 11, 2010, 9:06pm UTC](https://www.ruby-forum.com/t/super-simple-newbie-q-about-methods/188428 "2010-05-11T21:06:28Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![osshcs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/7c8e57/32.png) [@osshcs](https://www.ruby-forum.com/u/osshcs)\
**Post date:** [May 11, 2010, 9:06pm UTC](https://www.ruby-forum.com/t/super-simple-newbie-q-about-methods/188428/1 "2010-05-11T21:06:28Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![osshcs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/7c8e57/32.png) [@osshcs](https://www.ruby-forum.com/u/osshcs)\
**Post date:** [May 11, 2010, 9:47pm UTC](https://www.ruby-forum.com/t/super-simple-newbie-q-about-methods/188428/2 "2010-05-11T21:47:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![osshcs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/7c8e57/32.png) [@osshcs](https://www.ruby-forum.com/u/osshcs)\
**Post date:** [May 11, 2010, 10:30pm UTC](https://www.ruby-forum.com/t/super-simple-newbie-q-about-methods/188428/3 "2010-05-11T22:30:59Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![osshcs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/7c8e57/32.png) [@osshcs](https://www.ruby-forum.com/u/osshcs)\
**Post date:** [May 11, 2010, 11:01pm UTC](https://www.ruby-forum.com/t/super-simple-newbie-q-about-methods/188428/4 "2010-05-11T23:01:50Z")

</div>

> 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

---

<div class="post-metadata">

**Author:** ![osshcs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/7c8e57/32.png) [@osshcs](https://www.ruby-forum.com/u/osshcs)\
**Post date:** [May 11, 2010, 11:07pm UTC](https://www.ruby-forum.com/t/super-simple-newbie-q-about-methods/188428/5 "2010-05-11T23:07:03Z")

</div>

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? 🙂

---

<div class="post-metadata">

**Author:** ![osshcs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/7c8e57/32.png) [@osshcs](https://www.ruby-forum.com/u/osshcs)\
**Post date:** [May 11, 2010, 11:55pm UTC](https://www.ruby-forum.com/t/super-simple-newbie-q-about-methods/188428/6 "2010-05-11T23:55:43Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![osshcs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/7c8e57/32.png) [@osshcs](https://www.ruby-forum.com/u/osshcs)\
**Post date:** [May 11, 2010, 11:12pm UTC](https://www.ruby-forum.com/t/super-simple-newbie-q-about-methods/188428/7 "2010-05-11T23:12:06Z")

</div>

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,
