From: 7stud -- Date: 2007-09-28T04:44:20+09:00 Subject: Re: Ruby's a = b vs PHP5's $obj1 =& $obj2 > > 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 > => # > sysdebug(main):025:0> b = a > => # > 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. -- Posted via http://www.ruby-forum.com/.