From: "Hal E. Fulton" Date: 2003-06-14T03:31:08+09:00 Subject: Re: does each work on a copy? ----- Original Message ----- From: "Robert Klemme" Newsgroups: comp.lang.ruby To: "ruby-talk ML" Sent: Friday, June 13, 2003 5:31 AM Subject: Re: does each work on a copy? > > > It seems to hold a copy of the reference... > > > > No, it doesn't. When you do a: > > Sorry, but in fact you're wrong. The reference is copied indeed. > > > a = "Hello world, nice to meet you!".split(' ') > > a.each { |word| word = "lala" } > > p a > > ["Hello", "world,", "nice", "to", "meet", "you!"] > > > > You're binding word to a different object, not changing the objects > themselves. > > Binding "word" to another object is indeed done by copying the reference > to the other object. Or in other terms, after this assignment there is > one more reference to the object. I think part of the confusion is the way other languages do things. In C++, for example, assignment is a method, and a reference is an lvalue. So if you pass in a reference and assign to it, you do indeed change the object referred to. In Ruby, assignment is not a method, and an object reference is not an lvalue. Hal