Value type or reference type

Fixnum is value type, but whatever you pass into addOne() is passed by value, which means that a copy of the value is made.

If it is a reference type, a copy of the reference will be passed in. In that case, any change made to
the object pointed to by the reference will be reflected outside because the original reference value points to the same object.

A copy is always made when you pass a parameter to a method.
For value types, since the value is copied, there will be no side-effects.
For reference types, since a copy of the reference is passed in, methods called on the copied reference will affect the object and have side-effects.

gavri

···

-----Original Message-----
From: Sam Sungshik Kong [mailto:ssk@chol.nospam.net]
Subject: Value type or reference type

Hello!

In ruby FAQ, I find the following code.

def addOne(n)
n += 1
end
a = 1
addOne(a) # → 2
a # → 1

According to the manual, Fixnum is a value type.
Then the argument ‘a’ must be changed in-place.

Am I misunderstanding?