purpose of replace method

I

Ian Macdonald

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.
(e-mail address removed) |
http://www.caliban.org |
|
 
K

Kent Dahl

Ian said:
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.
 
I

Ian Macdonald

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.
(e-mail address removed) |
http://www.caliban.org |
|
 
A

Anders Borch

Ian said:
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?
 
S

Shashank Date

Anders Borch said:
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.
 
M

Mauricio Fernández

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
 
T

ts

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top