Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Question about string's in-place modification

Reply
Thread Tools

Question about string's in-place modification

 
 
Sam Kong
Guest
Posts: n/a
 
      06-04-2005
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.

How exactly does that work?

Thanks in advance.
Sam

 
Reply With Quote
 
 
 
 
Gennady Bystritsky
Guest
Posts: n/a
 
      06-04-2005
Just look into string.c in Ruby distribution. It's open source, after
all .

Gennady.

On Jun 4, 2005, at 12:10, 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.
>
> How exactly does that work?
>
> Thanks in advance.
> Sam
>
>




 
Reply With Quote
 
 
 
 
David A. Black
Guest
Posts: n/a
 
      06-04-2005
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
http://www.velocityreviews.com/forums/(E-Mail Removed)


 
Reply With Quote
 
Timothy Hunter
Guest
Posts: n/a
 
      06-04-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.
>
> How exactly does that work?
>
> Thanks in advance.
> Sam
>


Don't think of an object's id as a memory address. Think of it as a
handle for a memory address. That way, if the memory address of an
object needs to change (if, for example, Ruby needs to allocate more
memory for a string) Ruby can update the id with the new address and the
id can remain the same.
 
Reply With Quote
 
Lothar Scholz
Guest
Posts: n/a
 
      06-04-2005
Hello Sam,

SK> Hello!

SK> I wonder how destructive methods work in the behind.

SK> For example,

SK> s = '12345'
SK> s2 = s
SK> s.sub!('3', 'three')
puts s #->>'12three45'
puts s2 #->>'12three45'

SK> Destructive methods are in-place.
SK> Does that mean the object doesn't change its location in memory?
SK> Probably not.
SK> I don't think it's possible as the object might need more space and
SK> need to move to a bigger empty space for the changed data.
SK> If it moves, the references pointing to it should be updated as well.

SK> How exactly does that work?

A string is of c type VALUE (as every ruby object). This c type hold
at least 16 byte. So there is enough space to keep a string length and
the pointer inside the object.


SK> Thanks in advance.
SK> Sam




--
Best regards, emailto: scholz at scriptolutions dot com
Lothar Scholz http://www.ruby-ide.com
CTO Scriptolutions Ruby, PHP, Python IDE 's




 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Jar modification question Martin Gregorie Java 12 08-24-2010 08:14 AM
DNS Reply Modification (doctoring) intermittently failing Dav0 Cisco 1 01-28-2005 07:00 PM
Modification of Duty Cycle - Possible? Drew VHDL 2 08-02-2004 06:54 PM
Newbie question, Does variable need instance after its class modification? chenyu Python 1 10-27-2003 09:06 PM
modification : where can i find MOC ? bruno MCSE 3 09-19-2003 11:27 AM



Advertisments