Ruby's a = b vs PHP5's $obj1 =& $obj2

Just want to confirm Ruby’s

a = b

is the same as PHP5’s

$obj1 = $obj2

in other words, the reference is copied.

While

$obj1 =& $obj2

or

$obj1 = &$obj2

in both PHP4 and PHP5 isn’t the same as Ruby’s a = b

$obj1 and $obj2 just become synonyms…

and after

$obj1 = new Foo(“foo1”);
$obj2 =& $obj1
$obj2 = new Foo(“bar1”);

$obj1 and $obj2 have the same content, as they are just synonyms. (not a
clone, not same reference, but just synonyms). The “foo1” object is
garbage-collected since no one is referencing it.

this is giving me an initial headache… until maybe after i get used to
it.

On 9/27/07, SpringFlowers AutumnMoon [email protected] wrote:

$obj1 and $obj2 just become synonyms…

this is giving me an initial headache… until maybe after i get used to
it.

Posted via http://www.ruby-forum.com/.

IMO it’s giving you a headache because you’re trying to compare a clean,
well thought out language like Ruby to a hacked-together-over-many-years
language like PHP.

Ruby acts exactly as you would expect it to.

a = Object.new # => a now points to this object
a = b # => a and b now point to the same object
b = Object.new # => b now points to a new object, a still points to the
original object.

The faster you stop comparing to PHP and start thinking in terms of how
languages are supposed to work, the better.

Jason

On 9/27/07, SpringFlowers AutumnMoon [email protected] wrote:

While

this is giving me an initial headache… until maybe after i get used to
it.

in ruby:

a = Hash.new # a is a reference to Hash object #1
b = a # both a and b point to the same object
a[1] = ‘a’ # and b[1] is ‘a’ as well

but

b = Hash.new # now a points to Hash obj #1 and b to #2
b[1] = ‘c’ # a[1] is still ‘a’

i.e. assignment may destroy the link.

Quoth SpringFlowers AutumnMoon:

While

this is giving me an initial headache… until maybe after i get used to
it.

PHP’s $var1 =& $var2 seems to be setting a reference to a reference. I
think
ruby is more like PHP’s $var1 = $var2, though I could be wrong.

HTH,

SpringFlowers AutumnMoon wrote:

While

this is giving me an initial headache… until maybe after i get used to
it.

Hello,

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.

See an example:

sysdebug(main):014:0> class A
sysdebug(main):015:1> def setName(name)
sysdebug(main):016:2> @name = name
sysdebug(main):017:2> end
sysdebug(main):018:1> def putsName
sysdebug(main):019:2> puts @name
sysdebug(main):020:2> end
sysdebug(main):021:1> end
=> nil
sysdebug(main):022:0>
sysdebug(main):023:0*
sysdebug(main):024:0* a = A.new
A
=> #<A:0xb7cce1f8>
sysdebug(main):025:0> b = a
=> #<A:0xb7cce1f8>
sysdebug(main):026:0> a.setName(‘Jonas’)
=> “Jonas”
sysdebug(main):027:0> a.putsName
Jonas
=> nil
sysdebug(main):028:0> b.putsName
Jonas
=> nil
sysdebug(main):029:0> b.setName(‘Ana’)
=> “Ana”
sysdebug(main):030:0> a.putsName
Ana
=> nil

But in number types:

sysdebug(main):031:0> num1 = 1
=> 1
sysdebug(main):032:0> num2 = num1
=> 1
sysdebug(main):033:0> num2
=> 1
sysdebug(main):034:0> num1 = 2
=> 2
sysdebug(main):035:0> num2
=> 1
sysdebug(main):036:0>

Regards,

On Sep 27, 2007, at 10:34 AM, Jonas Roberto de Goes Filho (sysdebug)
wrote:

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.

There is no need to think of ‘numbers’ as a special case in Ruby.
And I don’t think your example even illustrates that they are
different.

=> 2
sysdebug(main):035:0> num2 # num2 still references the
object ‘1’
=> 1
sysdebug(main):036:0>

The trick is to think of integer literals not as values but as
references to objects. 1 is not the “value” 1 but is a reference
to the Fixnum object that behaves like the number 1 and there is
only a single Fixnum object that behaves that way (i.e. Fixnum objects
are not containers for integer values).

Note: Not everyone thinks of Ruby integer’s in this way but IMHO
it makes Ruby’s assignment semantics very simple and regular.

Gary W.

In PHP, $obj1 = & $obj2 means that both $obj1 and $obj2 points to the
same content. Its like hardlinks in Unix.

That is from C++. Those are “reference” semantics. It causes two names
to refer to the same spot in memory where a value is stored. Either
name can be used to change the value at that spot in memory, and since
both names always refer to that same spot in memory, either name can be
used to retrieve the new value there.

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.

See an example:

sysdebug(main):014:0> class A
sysdebug(main):015:1> def setName(name)
sysdebug(main):016:2> @name = name
sysdebug(main):017:2> end
sysdebug(main):018:1> def putsName
sysdebug(main):019:2> puts @name
sysdebug(main):020:2> end
sysdebug(main):021:1> end
=> nil
sysdebug(main):022:0>
sysdebug(main):023:0*
sysdebug(main):024:0* a = A.new
A
=> #<A:0xb7cce1f8>
sysdebug(main):025:0> b = a
=> #<A:0xb7cce1f8>
sysdebug(main):026:0> a.setName(‘Jonas’)
=> “Jonas”
sysdebug(main):027:0> a.putsName
Jonas
=> nil
sysdebug(main):028:0> b.putsName
Jonas
=> nil
sysdebug(main):029:0> b.setName(‘Ana’)
=> “Ana”
sysdebug(main):030:0> a.putsName
Ana
=> nil

But in number types:

sysdebug(main):031:0> num1 = 1
=> 1
sysdebug(main):032:0> num2 = num1
=> 1
sysdebug(main):033:0> num2
=> 1

The difference is this next step:

sysdebug(main):034:0> num1 = 2
=> 2
sysdebug(main):035:0> num2
=> 1
sysdebug(main):036:0>

You used assignment in that step. In other words, you reassigned to
num1. In your previous example you never used assignment to reassign
anything to a; instead you changed the object that a referred to.

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.