Hi --

On Thu, 28 Nov 2002, Shannon Fang wrote:

> Another problem about reference is that,
> if a is an object, let b=a, then b is a copy of the object or a pointer
> to a? I encountered the following problem
>
> a=b=c=Array.new
> d=e=f=""
>
> In my program, if I modify a, b and c will be affected, in another word,
> a, b, c point to the same Array. while I modify d, e and f are not
> affected, which means, strings are assigned by value... what will happen
> if I write d=e=f=String.new?
>
> Since everything in Ruby is object, I don't understand why String and
> Array are different...

It depends what you mean by "modify d".  I have a hunch you might be
doing something like:

  d = e = f = "some string"
  d = "some other string"

In that case, you're just reusing the name 'd' to refer to a new
object.  e and f don't care about that.

However, if you modifying the underlying object, then all references
to it will reflect those changes:

  d = e = f = "some string"
  d << " with three references"   # append to string, in place

  p e   # "some string with three references"


David

-- 
David Alan Black
home: dblack / candle.superlink.net
work: blackdav / shu.edu
Web:  http://pirate.shu.edu/~blackdav