From: "David A. Black" Date: 2007-08-21T01:24:14+09:00 Subject: Re: Assignment as a reference? Hi -- On Tue, 21 Aug 2007, Morgan Grubb wrote: > Hi, > > I ran into a simple issue earlier with assignment seeming to make > references. I'm new to Ruby so perhaps this is how it's supposed to be. Here It is. > is an abridged version of what happened: > > foo = 'bar' #=> bar > foo2 = foo #=> bar > foo = foo.gsub 'bar', 'wibble' #=> wibble > foo2 #=> bar > > foo = 'bar' #=> bar > foo2 = foo #=> bar > foo.gsub! 'bar', 'wibble' #=> wibble > foo2 #=> wibble > > You can paste those straight into irb and get the same output. Could someone > please explain why the second example changes foo2? Ruby assignments pretty much traffic in references. When you do: foo2 = foo you're giving foo2 a copy of the reference that foo contains. When you change the object, you see the change reflected through any reference to it. David -- * Books: RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242) RUBY FOR RAILS (http://www.manning.com/black) * Ruby/Rails training & consulting: Ruby Power and Light, LLC (http://www.rubypal.com)