[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

strip and its evil brother strip!

Aquila

3/19/2005 3:53:00 PM

Possibly a stupid question: why does strip! of a string with a single
character in it give nil and strip the single character?
I don't understand the behaviour of strip!...
--
"May the source be with you"
36 Answers

Glenn Parker

3/19/2005 4:07:00 PM

0

Aquila wrote:
> Possibly a stupid question: why does strip! of a string with a single
> character in it give nil and strip the single character?
> I don't understand the behaviour of strip!...

Looks like a bug to me.

$ ruby -e 'p " x ".strip!'
"x"

$ ruby -e 'p "x".strip!'
nil

--
Glenn Parker | glenn.parker-AT-comcast.net | <http://www.tetrafoi...


Florian Gross

3/19/2005 4:25:00 PM

0

Glenn Parker wrote:

> Aquila wrote:
>
>> Possibly a stupid question: why does strip! of a string with a single
>> character in it give nil and strip the single character? I don't
>> understand the behaviour of strip!...
>
>
> Looks like a bug to me.
>
> $ ruby -e 'p " x ".strip!'
> "x"
>
> $ ruby -e 'p "x".strip!'
> nil

This is by design. The destructive forms of built-in methods usually
return nil when they do nothing.

Jason Sweat

3/19/2005 4:33:00 PM

0

On Sun, 20 Mar 2005 01:07:15 +0900, Glenn Parker
<glenn.parker@comcast.net> wrote:
> Aquila wrote:
> > Possibly a stupid question: why does strip! of a string with a single
> > character in it give nil and strip the single character?
> > I don't understand the behaviour of strip!...
>
> Looks like a bug to me.
>
> $ ruby -e 'p " x ".strip!'
> "x"
>
> $ ruby -e 'p "x".strip!'
> nil

This came up on another thread recenly. It is a legacy behavior of the
*! functions that they return nil if nothing changed.

Regards,
Jason
http://blog.casey...


dblack

3/19/2005 4:57:00 PM

0

Daniel Amelang

3/19/2005 5:04:00 PM

0

I ranted about this very behavior 2 days ago. I'm willing to do an RCR
if anyone agrees (hint, hint).

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-t...


On Sun, 20 Mar 2005 01:56:40 +0900, David A. Black <dblack@wobblini.net> wrote:
> Hi --
>
> On Sun, 20 Mar 2005, Jason Sweat wrote:
>
> > On Sun, 20 Mar 2005 01:07:15 +0900, Glenn Parker
> > <glenn.parker@comcast.net> wrote:
> >> Aquila wrote:
> >>> Possibly a stupid question: why does strip! of a string with a single
> >>> character in it give nil and strip the single character?
> >>> I don't understand the behaviour of strip!...
> >>
> >> Looks like a bug to me.
> >>
> >> $ ruby -e 'p " x ".strip!'
> >> "x"
> >>
> >> $ ruby -e 'p "x".strip!'
> >> nil
> >
> > This came up on another thread recenly. It is a legacy behavior of the
> > *! functions that they return nil if nothing changed.
>
> I don't think it's legacy in the sense that that usually implies
> (something that's left over from an older design). That's just the
> way they behave.
>
> David
>
> --
> David A. Black
> dblack@wobblini.net
>
>


Gavin Kistner

3/19/2005 5:31:00 PM

0

On Mar 19, 2005, at 10:04 AM, Daniel Amelang wrote:
> I ranted about this very behavior 2 days ago. I'm willing to do an RCR
> if anyone agrees (hint, hint).
>
> http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-t...

I experience the same pain, and would vote for such an RCR



Glenn Parker

3/19/2005 5:39:00 PM

0

Florian Gross wrote:
>
> This is by design. The destructive forms of built-in methods usually
> return nil when they do nothing.

Yup, my bad. I know (really!) that this is by design, but I got myself
confused while writing such a simple reply.

For me, chaining methods is part of the Ruby way. Sacrificing the
ability to easily chain method! calls was a mistake, IMHO. I still trip
over this anomoly, and not once have I needed the functionality that
took its place.

--
Glenn Parker | glenn.parker-AT-comcast.net | <http://www.tetrafoi...


Florian Gross

3/19/2005 5:48:00 PM

0

Glenn Parker wrote:

>> This is by design. The destructive forms of built-in methods usually
>> return nil when they do nothing.
>
> Yup, my bad. I know (really!) that this is by design, but I got myself
> confused while writing such a simple reply.
>
> For me, chaining methods is part of the Ruby way. Sacrificing the
> ability to easily chain method! calls was a mistake, IMHO. I still trip
> over this anomoly, and not once have I needed the functionality that
> took its place.

I tend to disagree as destructive methods are not supposed to be
chained. They are optimized forms when you would be doing a variable
assignment, IMHO.

So they are optimizations for this case:

a = a.strip

But not for this case:

puts a.strip

While you might argue that modifying a String can be faster than
constructing a new one based on the old one, I still think that that is
not what matz had in mind when adding them. I'm not sure if this truly
is how this feature was meant to be used so it's probably best to take
this with a grain of salt until matz has clarified the situation.

Glenn Parker

3/19/2005 6:14:00 PM

0

Florian Gross wrote:
>
> I tend to disagree as destructive methods are not supposed to be
> chained. They are optimized forms when you would be doing a variable
> assignment, IMHO.
>
> So they are optimizations for this case:
>
> a = a.strip
>
> But not for this case:
>
> puts a.strip

Who (else) ever said destructive methods are not supposed to be chained?
I'm thinking more of this somewhat contrived, but broken, case:

words = gets.chomp!.strip!.downcase!.gsub!(/[^a-z]/, '').split(/ /)

Unless and until object creation overhead is greatly reduced, this is a
worthwhile idiom.

--
Glenn Parker | glenn.parker-AT-comcast.net | <http://www.tetrafoi...


Martin DeMello

3/19/2005 6:17:00 PM

0

Florian Gross <flgr@ccan.de> wrote:
> So they are optimizations for this case:
>
> a = a.strip

It'd be nice to similarly optimise

a.strip!.upcase!

without ever having to create and discard a new object.

martin