From: "Jonas Roberto de Goes Filho (sysdebug)" Date: 2007-09-27T23:34:20+09:00 Subject: Re: Ruby's a = b vs PHP5's $obj1 =& $obj2 --------------enigC3D91DCD06D6154B3B37054D Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: quoted-printable SpringFlowers AutumnMoon wrote: > Just want to confirm Ruby's >=20 > a =3D b >=20 > is the same as PHP5's >=20 > $obj1 =3D $obj2 >=20 > in other words, the reference is copied. >=20 > While >=20 > $obj1 =3D& $obj2 >=20 > or >=20 > $obj1 =3D &$obj2 >=20 > in both PHP4 and PHP5 isn't the same as Ruby's a =3D b >=20 > $obj1 and $obj2 just become synonyms... >=20 > and after >=20 > $obj1 =3D new Foo("foo1"); > $obj2 =3D& $obj1 > $obj2 =3D new Foo("bar1"); >=20 > $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. >=20 > this is giving me an initial headache... until maybe after i get used t= o > it. Hello, In PHP, $obj1 =3D & $obj2 means that both $obj1 and $obj2 points to the=20 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 =3D 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 =3D> nil sysdebug(main):022:0> sysdebug(main):023:0* sysdebug(main):024:0* a =3D A.new A =3D> # sysdebug(main):025:0> b =3D a =3D> # sysdebug(main):026:0> a.setName('Jonas') =3D> "Jonas" sysdebug(main):027:0> a.putsName Jonas =3D> nil sysdebug(main):028:0> b.putsName Jonas =3D> nil sysdebug(main):029:0> b.setName('Ana') =3D> "Ana" sysdebug(main):030:0> a.putsName Ana =3D> nil But in number types: sysdebug(main):031:0> num1 =3D 1 =3D> 1 sysdebug(main):032:0> num2 =3D num1 =3D> 1 sysdebug(main):033:0> num2 =3D> 1 sysdebug(main):034:0> num1 =3D 2 =3D> 2 sysdebug(main):035:0> num2 =3D> 1 sysdebug(main):036:0> Regards, --=20 Jonas Roberto de Goes Filho (sysdebug) http://goes.eti.br --------------enigC3D91DCD06D6154B3B37054D Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.6 (GNU/Linux) iD8DBQFG+79eaGIZR44LEAkRAgF+AJ4vgwezNHlFj/mmhuJ0Fuv+MNazhwCfRqDI slFm7YEd+BdF9ALfKsrPgxg= =xyAM -----END PGP SIGNATURE----- --------------enigC3D91DCD06D6154B3B37054D--