Question about string's in-place modification

Discussion in 'Ruby' started by Sam Kong, Jun 4, 2005.

  1. Sam Kong

    Sam Kong Guest

    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
    Sam Kong, Jun 4, 2005
    #1
    1. Advertising

  2. 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
    >
    >
    Gennady Bystritsky, Jun 4, 2005
    #2
    1. Advertising

  3. 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
    David A. Black, Jun 4, 2005
    #3
  4. 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.
    Timothy Hunter, Jun 4, 2005
    #4
  5. 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
    Lothar Scholz, Jun 4, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. umdehoon

    In-place array modification

    umdehoon, Jul 18, 2004, in forum: Python
    Replies:
    3
    Views:
    415
    Jeff Epler
    Jul 19, 2004
  2. Jim Britain

    string search and modification

    Jim Britain, Sep 6, 2006, in forum: Python
    Replies:
    3
    Views:
    291
    John Machin
    Sep 7, 2006
  3. MooMaster
    Replies:
    11
    Views:
    425
    MooMaster
    Apr 9, 2009
  4. RK Sentinel
    Replies:
    34
    Views:
    389
    Simon Krahnke
    Jan 28, 2009
  5. Dave Anderson

    In-place parameter modification

    Dave Anderson, Oct 30, 2009, in forum: Ruby
    Replies:
    16
    Views:
    170
    Rick DeNatale
    Nov 1, 2009
Loading...

Share This Page