Hi --

On Sun, 5 Jun 2005, Sam Kong wrote:

> Hello!
>
> I wonder how destructive methods work in the behind.
>
> For example,
>
> s = '12345'
> s2 = s
> s.sub!('3', 'three')
> puts s #->'12three45'
> puts s2 #->'12three45'
>
> Destructive methods are in-place.
> Does that mean the object doesn't change its location in memory?
> Probably not.
> I don't think it's possible as the object might need more space and
> need to move to a bigger empty space for the changed data.
> If it moves, the references pointing to it should be updated as well.

The references aren't the same as memory pointers, though.  They're a
Ruby language-level concept, probably overlapping with physical memory
allocation a lot of the time but also abstracted away from it.  In the
example above, I wouldn't care (except for possible speed
considerations) what was going on with memory management in the
interpreter, as long as I knew that on the Ruby side, I was operating
on a single object and it was behaving accordingly.


David

-- 
David A. Black
dblack / wobblini.net