Professional Web Applications Themes

purpose of replace method - Ruby

Hi, I was just wondering what the methods Array#replace, Hash#replace and String#replace achieve that can't be done with simple assignment. I'm going to have a guess and say that assignment of an array, a hash or a string to an already assigned variable creates a second object and then changes the variable to point to the new object, whereas the replace method does not cause a second object to be instantiated, but simply wipes out the old one. If my guess is right, then replace is there simply for the sake of efficiency, in the same way that foo << ...

  1. #1

    Default purpose of replace method

    Hi,

    I was just wondering what the methods Array#replace, Hash#replace and
    String#replace achieve that can't be done with simple assignment.

    I'm going to have a guess and say that assignment of an array, a hash or
    a string to an already assigned variable creates a second object and
    then changes the variable to point to the new object, whereas the
    replace method does not cause a second object to be instantiated, but
    simply wipes out the old one.

    If my guess is right, then replace is there simply for the sake of
    efficiency, in the same way that foo << bar is better than foo += bar.

    Well, I've just decided to put my theory to the test:

    #!/usr/bin/ruby -w

    require 'benchmark'
    include Benchmark

    n = 5000000
    bm(12) do |test|
    test.report('assignment:') do
    foo = "bar"
    1.upto(n) do
    foo = "foo"
    end
    end
    test.report('replacement:') do
    foo = "bar"
    1.upto(n) do
    foo.replace("foo")
    end
    end
    end

    user system total real
    assignment: 8.210000 0.000000 8.210000 ( 8.202920)
    replacement: 12.240000 0.010000 12.250000 ( 12.257115)

    So, it would seem that replacement is 50% less efficient than simple
    assignment.

    What, then, is the purpose of the replace method?

    Ian
    --
    Ian Macdonald | Far duller than a serpent's tooth it is to
    System Administrator | spend a quiet youth.
    [email]iancaliban.org[/email] |
    [url]http://www.caliban.org[/url] |
    |

    Ian Macdonald Guest

  2. #2

    Default Re: purpose of replace method

    Ian Macdonald wrote:
    >
    > Hi,
    >
    > I was just wondering what the methods Array#replace, Hash#replace and
    > String#replace achieve that can't be done with simple assignment.
    >
    > I'm going to have a guess and say that assignment of an array, a hash or
    > a string to an already assigned variable creates a second object and
    > then changes the variable to point to the new object, whereas the
    > replace method does not cause a second object to be instantiated, but
    > simply wipes out the old one.
    >
    > If my guess is right, then replace is there simply for the sake of
    > efficiency, in the same way that foo << bar is better than foo += bar.
    >
    > Well, I've just decided to put my theory to the test:
    >
    > #!/usr/bin/ruby -w
    >
    > require 'benchmark'
    > include Benchmark
    >
    > n = 5000000
    > bm(12) do |test|
    > test.report('assignment:') do
    > foo = "bar"
    > 1.upto(n) do
    > foo = "foo"
    > end
    > end
    > test.report('replacement:') do
    > foo = "bar"
    > 1.upto(n) do
    > foo.replace("foo")
    > end
    > end
    > end
    >
    > user system total real
    > assignment: 8.210000 0.000000 8.210000 ( 8.202920)
    > replacement: 12.240000 0.010000 12.250000 ( 12.257115)
    >
    > So, it would seem that replacement is 50% less efficient than simple
    > assignment.
    >
    > What, then, is the purpose of the replace method?
    For String#replace, this means that you can change a text you have
    spread to several places without doing a reassignment again. This can be
    handy if you have it a zillion places. (I.e. consider _one_ replace
    versus N assignments.)

    As for your test, I get the sinking feeling that the assignment part
    doesn't really measure anything useful. An assignment is pretty cheap,
    the "foo" is probably implemented with some kind of copy-on-write, while
    a replace has to write the source string over into the destination
    string byte-by-byte. Naturally, the latter would be slower.

    One potential efficiency of replace would be to reuse created objects to
    avoid creating new objects and using alot of memory. This kind of
    optimization is not measured by benchmark.

    --
    (\[ Kent Dahl ]/)_ _~_ _____[ [url]http://www.pvv.org/~kentda/[/url] ]_____/~
    ))\_student_/(( \__d L b__/ (pre-) Master of Science in Technology )
    ( \__\_ő|ő_/__/ ) _)Industrial economics and technological management(
    \____/_ö_\____/ (____engineering.discipline_=_Computer::Technology ___)
    Kent Dahl Guest

  3. #3

    Default Re: purpose of replace method

    On Fri 04 Jul 2003 at 19:35:21 +0900, Kent Dahl wrote:
    > For String#replace, this means that you can change a text you have
    > spread to several places without doing a reassignment again. This can be
    > handy if you have it a zillion places. (I.e. consider _one_ replace
    > versus N assignments.)
    Ah, I see. So:

    irb(main):001:0> foo = bar = "string"
    => "string"
    irb(main):002:0> foo = "new"
    => "new"
    irb(main):003:0> bar
    => "string"
    irb(main):004:0> foo = bar = "string"
    => "string"
    irb(main):005:0> foo.replace("new")
    => "new"
    irb(main):006:0> bar
    => "new"

    OK, that makes sense. replace is useful when multiple pointers point to
    the same object.

    Ian
    --
    Ian Macdonald | Never try to keep up with the Joneses; they
    System Administrator | might be newlyweds.
    [email]iancaliban.org[/email] |
    [url]http://www.caliban.org[/url] |
    |

    Ian Macdonald Guest

  4. #4

    Default Re: purpose of replace method

    Ian Macdonald wrote:
    > On Fri 04 Jul 2003 at 19:35:21 +0900, Kent Dahl wrote:
    >
    >
    >>For String#replace, this means that you can change a text you have
    >>spread to several places without doing a reassignment again. This can be
    >>handy if you have it a zillion places. (I.e. consider _one_ replace
    >>versus N assignments.)
    >
    >
    > Ah, I see. So:
    >
    > irb(main):001:0> foo = bar = "string"
    > => "string"
    > irb(main):002:0> foo = "new"
    > => "new"
    > irb(main):003:0> bar
    > => "string"
    > irb(main):004:0> foo = bar = "string"
    > => "string"
    > irb(main):005:0> foo.replace("new")
    > => "new"
    > irb(main):006:0> bar
    > => "new"
    >
    > OK, that makes sense. replace is useful when multiple pointers point to
    > the same object.
    >
    for this purpose it would be nice to have in Object. Why isn't there a
    replace method in Object?

    --
    dc -e
    4dd*od3*dddn1-89danrn10-dan3+ann6*dan*2*an13dn1+dn2-dn3+5*ddan2/9+an13nap

    Anders Borch Guest

  5. #5

    Default Re: purpose of replace method


    "Anders Borch" <spamdeck.dk> wrote in message
    > for this purpose it would be nice to have in Object. Why isn't there a
    > replace method in Object?
    Array, Hash and String are containers. Object is not necessarily a
    container.


    Shashank Date Guest

  6. #6

    Default Re: purpose of replace method

    On Fri, Jul 04, 2003 at 10:36:08PM +0900, Shashank Date wrote:
    >
    > "Anders Borch" <spamdeck.dk> wrote in message
    > > for this purpose it would be nice to have in Object. Why isn't there a
    > > replace method in Object?
    >
    > Array, Hash and String are containers. Object is not necessarily a
    > container.
    I believe he wants #become.

    --
    _ _
    | |__ __ _| |_ ___ _ __ ___ __ _ _ __
    | '_ \ / _` | __/ __| '_ ` _ \ / _` | '_ \
    | |_) | (_| | |_\__ \ | | | | | (_| | | | |
    |_.__/ \__,_|\__|___/_| |_| |_|\__,_|_| |_|
    Running Debian GNU/Linux Sid (unstable)
    batsman dot geo at yahoo dot com

    'Ooohh.. "FreeBSD is faster over loopback, when compared to Linux
    over the wire". Film at 11.'
    -- Linus Torvalds

    Mauricio Fernández Guest

  7. #7

    Default Re: purpose of replace method

    Mauricio Fernández wrote:
    > On Fri, Jul 04, 2003 at 10:36:08PM +0900, Shashank Date wrote:
    >
    >>"Anders Borch" <spamdeck.dk> wrote in message
    >>
    >>>for this purpose it would be nice to have in Object. Why isn't there a
    >>>replace method in Object?
    >>
    >>Array, Hash and String are containers. Object is not necessarily a
    >>container.
    >
    >
    > I believe he wants #become.
    >
    yep, but that's not in Object either.

    --
    dc -e
    4dd*od3*dddn1-89danrn10-dan3+ann6*dan*2*an13dn1+dn2-dn3+5*ddan2/9+an13nap

    Anders Borch Guest

  8. #8

    Default Re: purpose of replace method

    >>>>> "w" == why the lucky stiff <ruby-talkwhytheluckystiff.net> writes:

    w> See [ruby-core:1164]. I guess `become' was added, then disappeared.

    A method *with the name* #become was added, then renamed in
    #initialize_copy, but never this method made something similar to the
    original #become

    w> Cited as being too dangerous.

    Just look at [ruby-talk:19761], if you want to know *one* of the *many*
    problems that this method has.


    Guy Decoux

    ts Guest

Similar Threads

  1. Purpose of Contribute
    By Seraya in forum Macromedia Contribute General Discussion
    Replies: 2
    Last Post: April 13th, 02:48 PM
  2. Search and replace (super global replace)
    By johnweiffenbach@adobeforums.com in forum Adobe Acrobat Windows
    Replies: 1
    Last Post: April 8th, 08:56 AM
  3. RegExp Replace Method causes Access Violation
    By Alex in forum ASP Components
    Replies: 2
    Last Post: November 17th, 10:30 AM
  4. General Purpose Transactions
    By T. Onoma in forum Ruby
    Replies: 2
    Last Post: November 14th, 01:28 AM
  5. purpose of threads?
    By Paul Archer in forum PERL Miscellaneous
    Replies: 0
    Last Post: July 9th, 02:35 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139