From: dblack@... Date: 2002-10-25T02:20:37+09:00 Subject: Re: Things That Newcomers to Ruby Should Know (10/16/02) Hi -- On Fri, 25 Oct 2002, Austin Ziegler wrote: > 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 I think that's kind of a roundabout way to describe it though. I would say: it's creating a new object, and assigning a reference to that object to a variable which previously held a reference to a different object. > 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. Right -- what I was saying was just that "intermediate object" didn't sound like the right term to me, just as when you say: a = "Hello" you're just creating an object (which I would not consider an intermediate object). I guess I think of intermediate object creation as, for instance: a = [1,2,3] b = a.map {|x| x * 2} .map {|x| x * 3} where there's an anonymous array being created en route to the eventual creation of the array referred to by 'b'. David -- David Alan Black home: dblack@candle.superlink.net work: blackdav@shu.edu Web: http://pirate.shu.edu/~blackdav