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

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

···

--
Posted via http://www.ruby-forum.com/.

Hi --

···

On Fri, 10 Sep 2010, Geometric Patterns wrote:

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

What you're likely running into is that .rstrip! returns nil if no
change was made.

Check this for more details:
http://ruby-doc.org/core/classes/String.html#M000829

-Alex

···

On Thu, 2010-09-09 at 19:23 -0500, Geometric Patterns wrote:

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

Geometric Patterns wrote:

Rather than:

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

You could do:

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

···

--
Posted via http://www.ruby-forum.com/\.

chomp is not necessary if followed by rstrip.

···

At 2010-09-09 08:23PM, "Geometric Patterns" wrote:

Rather than:

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

--
Glenn Jackman
    Write a wise saying and your name will live forever. -- Anonymous

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

foo.chomp!.rstrip!.downcase!

you can.

%w( chomp! rstrip! downcase! ).each{|m| foo.send(m)}

-a

Alex Stahl wrote:

What you're likely running into is that .rstrip! returns nil if no
change was made.

Check this for more details:
class String - RDoc Documentation

-Alex

Thank you David and Alex!

···

--
Posted via http://www.ruby-forum.com/\.

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

foo = foo.chomp.rstrip.downcase

/lasso

···

On 10 Sep, 09:31, Brian Candler <b.cand...@pobox.com> wrote:

Geometric Patterns wrote:

> Rather than:

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

You could do:

foo.instance_eval { chomp!; rstrip!; downcase! }
--
Posted viahttp://www.ruby-forum.com/.

So... what's wrong with:

foo = foo.chomp.rstrip.downcase

Jason

···

On Sep 10, 2010, at 11:28 AM, ara.t.howard wrote:

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

foo.chomp!.rstrip!.downcase!

you can.

%w( chomp! rstrip! downcase! ).each{|m| foo.send(m)}

-a

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

···

On Fri, Sep 10, 2010 at 11:26 AM, Jason Roelofs <jameskilton@gmail.com>wrote:

On Sep 10, 2010, at 11:28 AM, ara.t.howard wrote:

>
>
>
>>
>> ...it seems like I should be able to do:
>>
>> foo.chomp!.rstrip!.downcase!
>>
>
> you can.
>
> %w( chomp! rstrip! downcase! ).each{|m| foo.send(m)}
>
> -a
>

So... what's wrong with:

foo = foo.chomp.rstrip.downcase

Jason

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

Jesus.

···

On Fri, Sep 10, 2010 at 6:26 PM, Jason Roelofs <jameskilton@gmail.com> wrote:

On Sep 10, 2010, at 11:28 AM, ara.t.howard wrote:

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

foo.chomp!.rstrip!.downcase!

you can.

%w( chomp! rstrip! downcase! ).each{|m| foo.send(m)}

-a

So... what's wrong with:

foo = foo.chomp.rstrip.downcase

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

···

On Sep 10, 2010, at 1:29 PM, Jesús Gabriel y Galán wrote:

On Fri, Sep 10, 2010 at 6:26 PM, Jason Roelofs <jameskilton@gmail.com> wrote:

On Sep 10, 2010, at 11:28 AM, ara.t.howard wrote:

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

foo.chomp!.rstrip!.downcase!

you can.

%w( chomp! rstrip! downcase! ).each{|m| foo.send(m)}

-a

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.

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

foo.chomp!.rstrip!.downcase!

you can.

%w( chomp! rstrip! downcase! ).each{|m| foo.send(m)}

-a

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.

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.

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 Klemme wrote:

... 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.

···

On Fri, Sep 10, 2010 at 8:49 PM, Jason Roelofs <jameskilton@gmail.com> wrote:

On Sep 10, 2010, at 1:29 PM, Jesús Gabriel y Galán wrote:

On Fri, Sep 10, 2010 at 6:26 PM, Jason Roelofs <jameskilton@gmail.com> wrote:

On Sep 10, 2010, at 11:28 AM, ara.t.howard wrote:

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

···

On 10 Sep, 23:11, Jesús Gabriel y Galán <jgabrielyga...@gmail.com> wrote:

On Fri, Sep 10, 2010 at 8:49 PM, Jason Roelofs <jameskil...@gmail.com> wrote:
> On Sep 10, 2010, at 1:29 PM, Jesús Gabriel y Galán wrote:

>> On Fri, Sep 10, 2010 at 6:26 PM, Jason Roelofs <jameskil...@gmail.com> wrote:
>>> On Sep 10, 2010, at 11:28 AM, ara.t.howard wrote:

>>>>> ...it seems like I should be able todo:

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

>>>> you can.

>>>> %w( chomp!rstrip!downcase! ).each{|m| foo.send(m)}

>>>> -a

>>> 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.

> 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.

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 Klemme wrote:
> ... 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.

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

/lasso

···

On 12 Sep, 15:13, Lars Olsson <la...@lassoweb.se> wrote:

On 10 Sep, 23:11, Jesús Gabriel y Galán <jgabrielyga...@gmail.com> > wrote:

> On Fri, Sep 10, 2010 at 8:49 PM, Jason Roelofs <jameskil...@gmail.com> wrote:
> > On Sep 10, 2010, at 1:29 PM, Jesús Gabriel y Galán wrote:

> >> On Fri, Sep 10, 2010 at 6:26 PM, Jason Roelofs <jameskil...@gmail.com> wrote:
> >>> On Sep 10, 2010, at 11:28 AM, ara.t.howard wrote:

> >>>>> ...it seems like I should be able todo:

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

> >>>> you can.

> >>>> %w( chomp!rstrip!downcase! ).each{|m| foo.send(m)}

> >>>> -a

> >>> 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.

> > 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.

> 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 Klemme wrote:
> > ... 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.

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