Jonas Roberto de Goes Filho (sysdebug) wrote:
In PHP, $obj1 = & $obj2 means that both $obj1 and $obj2 points to the
same content. Its like hardlinks in Unix.
In Ruby, depends of type of variable. If type of variable as object, its
work like as PHP. If type of variable as a number, its not work as PHP.
The first pargraph may or may not be correct. I do not know, since I
know nothing about PHP and little about UNIX.
In Ruby, depends of type of variable. If type of variable as object,
its
work like as PHP. If type of variable as a number, its not work as
PHP.
The second cannot be correct. Variables in Ruby do not have a type,
they reference objects. Moreover, when b is a variable
a = b
always means the same thing, and that does not depend on the type
of object referenced by b!
The line (a = b) means that the variables, a and b, reference the same
object UNTIL either reference is changed. Changing the reference to
one will mean they no longer refer to the same object. Changing some
reference within the object will not change what either variable
refers to, BUT the common object referred to is modified. This is
true for all types of objects in Ruby.
In Jonas’ example, the difference is that the object created by
a = A.new
is capable of having a name and the line
a.setName(‘Jonas’)
creates a reference to a string within that object. Since a and b
still refer to the same object either one can change the reference,
@name, i.e. they can change the name and then both a and b will know
about that change. Once either a or b refers to a different object
the link or common reference for a and b is broken!
The Fixnum class is different from the A class in that the only way to
change the value of a Fixnum object is to create a new object and
hence a new reference; while in A changing the name does not create a
new object, it modifies the object already created.
I think the basic problem is that in many languages variables refer to
a particular place in memory with a given structure. Ruby on the
other hand makes a variable refer to an object not a place in memory.
Anyone coming from the first type of language to Ruby has a big
adjustment to make in handling the language. (I know I did when I
first looked at Python.) Often it doesn’t seem to matter, but that
just makes the adjustment harder because the difference is less
visible.
Ian W.