How to do foo.chomp!.rstrip!.downcase! ?

G

Geometric Patterns

Rather than:

foo.chomp!
foo.rstrip!
foo.downcase!

...it seems like I should be able to do:

foo.chomp!.rstrip!.downcase!

...but I get: undefined method `downcase!' for nil:NilClass
(NoMethodError)

This is probably a basic ruby thing that I'm not grokking...

Many thanks in advance,

- d
 
D

David A. Black

Hi --

Rather than:

foo.chomp!
foo.rstrip!
foo.downcase!

...it seems like I should be able to do:

foo.chomp!.rstrip!.downcase!

...but I get: undefined method `downcase!' for nil:NilClass
(NoMethodError)

This is probably a basic ruby thing that I'm not grokking...

A lot of destructive ! methods return nil if the object isn't changed --
for example:

"abc".gsub!(/z/, "y")
=> nil

So it's generally best not to chain them.


David

--
David A. Black, Senior Developer, Cyrus Innovation Inc.

The Ruby training with Black/Brown/McAnally
Compleat Philadelphia, PA, October 1-2, 2010
Rubyist http://www.compleatrubyist.com
 
L

Lars Olsson

You could do:

foo.instance_eval { chomp!; rstrip!; downcase! }

....or just don't use the "bang" versions of the methods:

foo = foo.chomp.rstrip.downcase

/lasso
 
J

Josh Cheek

[Note: parts of this message were removed to make it a legal post.]

So... what's wrong with:

foo = foo.chomp.rstrip.downcase

Jason
What is the rationale behind having them return nil if unchanged?
 
J

Jesús Gabriel y Galán

So... what's wrong with:

foo = foo.chomp.rstrip.downcase

Each call creates a copy of the string, and are all discarded along
with the original except the last one.

Jesus.
 
J

Jason Roelofs

=20
Each call creates a copy of the string, and are all discarded along
with the original except the last one.
=20
Jesus.
=20

I don't see any problem there, that's expected. Ruby has a GC, and those =
will get cleaned up appropriately.

The ! versions don't work chained, this version does. You shouldn't be =
worrying about the cost of creating 3 strings.

Jason=
 
J

Jesús Gabriel y Galán

I don't see any problem there, that's expected. Ruby has a GC, and those =
will get cleaned up appropriately.
The ! versions don't work chained, this version does. You shouldn't be wo=
rrying about the cost of creating 3 strings.

I know, regarding that, I don't see any problem either with

foo.chomp!
foo.rstrip!
foo.downcase!

Robert commented on this already (although his email was lost in some
problem with SpamAssassin, but he resent):

Robert said:
... which usually is less efficient. I guess OP had a reason to
use bang versions.

and I clarified. If it's three strings, no problem. If this is in a
loop you might want to reduce garbage. Just pointing out a fact about
the non-bang methods.

Jesus.
 
L

Lars Olsson

I know, regarding that, I don't see any problem either with

foo.chomp!
foo.rstrip!
foo.downcase!

Robert commented on this already (although his email was lost in some
problem with SpamAssassin, but he resent):



and I clarified. If it's three strings, no problem. If this is in a
loop you might want to reduce garbage. Just pointing out a fact about
the non-bang methods.

Jesus.

Yeah, looking at the source code for ruby 1.9 reveals that
String.chomp is essentially implemented as String.sup.chomp!, so
you're absolutely right about the non bang versions creating some
extra garbage. The same is true about (rstrip and downcase as well.)
How this affects the overall performance is really hard to say without
benchmarking though. But as Robert claimed

foo.chomp!
foo.rstrip!
foo.downcase!

is probable the most efficient way.

/lasso
 
L

Lars Olsson

Yeah, looking at the source code for ruby 1.9 reveals that
String.chomp is essentially implemented as String.sup.chomp!, so
you're absolutely right about the non bang versions creating some
extra garbage. The same is true about (rstripanddowncaseas well.)
How this affects the overall performance is really hard to say without
benchmarking though. But as Robert claimed

foo.chomp!
foo.rstrip!
foo.downcase!

is probable the most efficient way.

/lasso

Argh...it was supposed to read "implemented as String.dup.chomp!"

/lasso
 

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

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,826
Members
47,371
Latest member
Brkaa

Latest Threads

Top