Method Chaining Issues

H

Hal Fulton

Kyle said:
I'm new to Ruby so I when you refer to a "bang" method you mean something
like "chomp!"? And if so, why do some thing they shouldn't exist? Just
curious.

It's not a Ruby thing... "bang" is just common programmer slang
for the exclamation point.


Hal
 
H

Hal Fulton

Kyle said:
I'm new to Ruby so I when you refer to a "bang" method you mean something
like "chomp!"? And if so, why do some thing they shouldn't exist? Just
curious.

Oops, failed to address the other half of that.

The ! usually (not always) indicates that a method changes its
receiver "in place" rather than creating a new object as a result.

I gather that functional programming people dislike this -- but I
have never used a real FP language (such as Haskell?).


Hal
 
G

Gyoung-Yoon Noh

Eric, thank you for kind explanation. It helps me.
I have been reading ruby source code with very lazy mode.
I need a kind of booster reading engine. ;-)

On 01 Jun 2005, at 18:36, Gyoung-Yoon Noh wrote:
=20
=20
The difference is not in the speed of the operations themselves, it
is in the memory pressure on the GC. Your benchmark is not a fair
comparison between the two because it ignores the side-effect of
memory pressure and does not fit with the way you would use chaining
vs !.
=20
This benchmark more realisticly shows the effects of memory pressure
and is more fitting with how you would really use chaining vs !:
=20
$ cat sub.rb
require 'benchmark'
=20
N =3D 10_000
STR =3D (0..N).to_a.join('-')
=20
Benchmark.bmbm do |bm|
bm.report("Base") do
str =3D STR.dup
N.times { }
end
=20
bm.report("Destructive") do
str =3D STR.dup
N.times { str.sub!(/\d+/, '-') }
end
=20
bm.report("Non-destructive") do
str =3D STR.dup
N.times { str =3D str.sub(/\d+/, '-') }
end
end
=20
$ ruby sub.rb
Rehearsal ---------------------------------------------------
Base 0.000000 0.000000 0.000000 ( 0.003034)
Destructive 1.470000 1.380000 2.850000 ( 3.266492)
Non-destructive 1.930000 2.740000 4.670000 ( 5.584387)
------------------------------------------ total: 7.520000sec
=20
user system total real
Base 0.000000 0.000000 0.000000 ( 0.002857)
Destructive 1.460000 1.400000 2.860000 ( 3.383933)
Non-destructive 1.910000 2.800000 4.710000 ( 5.688635)
=20
=20
=20
Your benchmark wasn't helpful in revealing the true difference
between the two methods. As you can see, the non-destructive case
takes much more time in user space because the GC has to clean up all
the temporary strings.
=20
It also takes ~33% more time in general because of the GC.
=20
=20
It wraps a C string.
=20
=20
Certain operations are COW.
=20
--
Eric Hodel - (e-mail address removed) - http://segment7.net
FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04
=20
=20
=20


--=20
http://nohmad.sub-port.net
 
M

Martin DeMello

Nikolai Weibull said:
No it?s not. This is an argument for keeping Ruby non-functional. This
has nothing to do with String#sub vs. String#sub! or Array#sort vs.
Array#sort! for that matter. When discussing destructive methods with
bangs, we?re discussing the merit of having String#sub! around for
efficiency reasons. It?s not a discussion of whether to allow objects
to modify their internal state or not. I know you know this and I know
what you?re trying to say, but this doesn?t lead anywhere,

Note that string.meth! is not an optimisation of string.meth, it's an
optimisation of string.replace(string.sort). There's a difference when
there are multiple references to the string.

martin
 
L

Logan Capaldo

=20
Oops, failed to address the other half of that.
=20
The ! usually (not always) indicates that a method changes its
receiver "in place" rather than creating a new object as a result.

Just curious, can you give me an example of a bang method that doesn't
modify the
receiver in place?
 
H

Hal Fulton

Logan said:
Just curious, can you give me an example of a bang method that doesn't
modify the
receiver in place?

The only one in the core is exit! as far as I know.

Some things like Kernel#chomp! have an implicit receiver,
so they don't really make my point.

There may be others in the std lib or other common libs,
I don't know.

The point is that the ! is to mark a method as "dangerous"
or "requiring caution."

I certainly use it in my own code in situations where it doesn't
change the receiver. In fact, I encourage others to do the
same, as it helps dispel the minor misconception. Just my
opinion.


Hal
 

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,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top