On 5/5/06, William (Bill) Froelich <wfroelich / dbsnow.com> wrote:> I think I'm might be missing something here.>> In the examples above by just reading the code I would have originally> expected a to return "foo" in both examples (which it does for me> running it through irb).>> But after thinking about it a little more, if you use gsub! the first> example does return a #=> "bar" (just a typo I think)
Yes. That's a typo.
> Please correct me if I am off base here but what I think is happening is> that when we set a = x what we are really doing is creating a reference> a to the same object that x points to (which happens to be "foo").  Then> when we modify the object that x is referencing (changing foo to bar) a> also changes since it points to the same object.
Yes, but that's overthinking it. Jim Weirich's "Shoeboxes andBindings" article[1] helps here.
Variables are labels. Constants are just special variables. Variablesin Ruby are different than they are in any other language I've used.Variables in Ruby are pure references and do not represent any storagespace in and of themselves. Variables are simply "the name by which weoperate on an object". So when I do:
  x = "bar"  a = x
I have said that the particular instance of string "bar" is able to bereferred to by the name x. And then I've said that the object referredto by the name x can be referred to by the name a. So now we have oneobject with two names.
If you want a copy, you have to either #dup or #clone the object.
The object is the atomic element here.
-austin[1] http://onestepback.org/index.cgi/2003/12/15--Austin Ziegler * halostatue / gmail.com               * Alternate: austin / halostatue.ca