Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > strip and its evil brother strip!

Reply
Thread Tools

strip and its evil brother strip!

 
 
Aquila
Guest
Posts: n/a
 
      03-19-2005
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"
 
Reply With Quote
 
 
 
 
Glenn Parker
Guest
Posts: n/a
 
      03-19-2005
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.tetrafoil.com/>


 
Reply With Quote
 
 
 
 
Florian Gross
Guest
Posts: n/a
 
      03-19-2005
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.
 
Reply With Quote
 
Jason Sweat
Guest
Posts: n/a
 
      03-19-2005
On Sun, 20 Mar 2005 01:07:15 +0900, 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 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-sweat.us/


 
Reply With Quote
 
David A. Black
Guest
Posts: n/a
 
      03-19-2005
Hi --

On Sun, 20 Mar 2005, Jason Sweat wrote:

> On Sun, 20 Mar 2005 01:07:15 +0900, 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 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



 
Reply With Quote
 
Daniel Amelang
Guest
Posts: n/a
 
      03-19-2005
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...by-talk/133894


On Sun, 20 Mar 2005 01:56:40 +0900, David A. Black <> wrote:
> Hi --
>
> On Sun, 20 Mar 2005, Jason Sweat wrote:
>
> > On Sun, 20 Mar 2005 01:07:15 +0900, 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 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
>
>
>



 
Reply With Quote
 
Gavin Kistner
Guest
Posts: n/a
 
      03-19-2005
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...by-talk/133894


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



 
Reply With Quote
 
Glenn Parker
Guest
Posts: n/a
 
      03-19-2005
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.tetrafoil.com/>


 
Reply With Quote
 
Florian Gross
Guest
Posts: n/a
 
      03-19-2005
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.
 
Reply With Quote
 
Glenn Parker
Guest
Posts: n/a
 
      03-19-2005
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.tetrafoil.com/>


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Its a bird, its a plane, no ummm, its a Ruide thunk Ruby 1 03-30-2010 11:10 AM
strip all but second second line from bottom and then strip that!!!! yelipolok Perl Misc 4 01-27-2010 08:14 AM
Brother MFC 7820N - Brother can't help Chaudhry Nijjhar Computer Support 1 11-03-2006 04:04 PM
Evil, evil wxPython (and a glimmer of hope) vivainio@gmail.com Python 12 02-17-2006 07:49 PM
Regex to strip evil HTML tags Daniel M. Hendricks ASP .Net 2 04-11-2005 02:21 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57