From: Austin Ziegler Date: 2002-10-25T02:06:45+09:00 Subject: Re: Things That Newcomers to Ruby Should Know (10/16/02) On Thu, 24 Oct 2002 20:21:54 +0900, dblack@candle.superlink.net wrote: > On Thu, 24 Oct 2002, William Djaja Tjokroaminata wrote: >> Therefore, a person with a C++ background, who is used to >> avoiding the creation of unnecessary intermediate objects, will >> be surprised that Ruby does it *all the time* in its '+=' >> operator. (Furthermore, the creation of equivalent intermediate >> objects may even be more "expensive" in Ruby than in C++ in >> absolute terms.) > I don't think "a += 'str'" (or whatever) is creating an intermediate > object though, at least not in the sense that I understand the term. > It's just performing an assignment, not doing anything extra. It's creating an intermediate object that then becomes the original object, in a sense. I think that the C++ programmer -- where references are often used like "safe" or "pretty" pointers, would expect the following code to print out the same Object#ID both times -- and it doesn't. It doesn't because you're creating a composite object (a + "bar") and then making a refer to that object rather than the original a. a = "Foo" puts "a.id: #{a.id}" a += "bar" puts "a.id: #{a.id}" I can see where it might be confusing, but as long it's made clear that a += b is the same as a = a + b, then even C++ programmers shouldn't expect a to point to the same object any longer (because '+' would likely create an intermediate object). -austin -- Austin Ziegler, austin@halostatue.ca on 2002.10.24 at 12.59.45