# Destructive! operations

**URL:** <https://rubytalk.org/t/destructive-operations/16625>\
**Category:** ruby-talk\
**Created:** [20 February 2005 08:22 UTC](https://rubytalk.org/t/destructive-operations/16625 "2005-02-20T08:22:34Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Navindra\_Umanee](https://avatars.discourse-cdn.com/v4/letter/n/d78d45/32.png) [@Navindra\_Umanee](https://rubytalk.org/u/Navindra_Umanee)\
**Post date:** [20 February 2005 08:22 UTC](https://rubytalk.org/t/destructive-operations/16625/1 "2005-02-20T08:22:34Z")

</div>

Hi,

Is it more efficient to use the destructive versions of functions in  
Ruby? I know that in Lisp/Scheme destructive counterparts are usually  
offered for efficiency reasons.

Can I assume that string.gsub! is preferable to string.gsub when I  
know that the side-effect won't be affecting any other code?

I find the Ruby destructive operations sometimes inconvenient to use.  
For example strip! might return null where strip would return a  
string. However, if it's more efficient, I'd rather use the  
destructive version.

Thanks,  
Navin.

---

<div class="post-metadata">

**Author:** ![Yukihiro\_Matsumoto2](https://avatars.discourse-cdn.com/v4/letter/y/b9e5f3/32.png) [@Yukihiro\_Matsumoto2](https://rubytalk.org/u/Yukihiro_Matsumoto2)\
**Post date:** [20 February 2005 14:24 UTC](https://rubytalk.org/t/destructive-operations/16625/2 "2005-02-20T14:24:55Z")

</div>

Hi

> **···**
>
> In message "Re: destructive! operations" on Sun, 20 Feb 2005 17:22:34 +0900, Navindra Umanee \<navindra@cs.mcgill.ca\> writes:
> 
> > Is it more efficient to use the destructive versions of functions in  
> > Ruby? I know that in Lisp/Scheme destructive counterparts are usually  
> > offered for efficiency reasons.
> > 
> > Can I assume that string.gsub! is preferable to string.gsub when I  
> > know that the side-effect won't be affecting any other code?
> 
> Although most (not all) of bang methods are more efficient than their  
> counterparts, I discourage the use of them unless the performance is  
> really problem. "Premature optimization is the source of all evil".
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;matz.

---

<div class="post-metadata">

**Author:** ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://rubytalk.org/u/Robert)\
**Post date:** [20 February 2005 15:04 UTC](https://rubytalk.org/t/destructive-operations/16625/3 "2005-02-20T15:04:44Z")

</div>

"Yukihiro Matsumoto" \<matz@ruby-lang.org\> schrieb im Newsbeitrag news:1108909479.514053.4734.nullmailer@x31.priv.netlab.jp...

> Hi
> 
> \>Is it more efficient to use the destructive versions of functions in  
> \>Ruby? I know that in Lisp/Scheme destructive counterparts are usually  
> \>offered for efficiency reasons.  
> \>  
> \>Can I assume that string.gsub! is preferable to string.gsub when I  
> \>know that the side-effect won't be affecting any other code?
> 
> Although most (not all) of bang methods are more efficient than their  
> counterparts, I discourage the use of them unless the performance is  
> really problem. "Premature optimization is the source of all evil".

Although I agree to the latter statement, I beg to differ on the general remark - at least a bit. I often use this

while ( line = gets )  
&nbsp;&nbsp;line.chomp!  
&nbsp;&nbsp;# work with line  
end

because I know it's going to be potentially invoked often - even in scripts that are likely to run only on small input files. But for large files where not every line is processed it easily divides the number of instances created by a factor of 2.

Kind regards

&nbsp;&nbsp;&nbsp;&nbsp;robert

> **···**
>
> > In message "Re: destructive! operations" \> on Sun, 20 Feb 2005 17:22:34 +0900, Navindra Umanee \> \<navindra@cs.mcgill.ca\> writes:

---

<div class="post-metadata">

**Author:** ![Caio\_Tiago\_Oliveira](https://avatars.discourse-cdn.com/v4/letter/c/f17d59/32.png) [@Caio\_Tiago\_Oliveira](https://rubytalk.org/u/Caio_Tiago_Oliveira)\
**Post date:** [20 February 2005 16:23 UTC](https://rubytalk.org/t/destructive-operations/16625/4 "2005-02-20T16:23:48Z")

</div>

Robert Klemme, 20/2/2005 12:04:

> "Yukihiro Matsumoto" \<matz@ruby-lang.org\> schrieb im Newsbeitrag news:1108909479.514053.4734.nullmailer@x31.priv.netlab.jp...
> 
> > \>Is it more efficient to use the destructive versions of functions in  
> > \>Ruby? I know that in Lisp/Scheme destructive counterparts are usually  
> > \>offered for efficiency reasons.  
> > \>  
> > \>Can I assume that string.gsub! is preferable to string.gsub when I  
> > \>know that the side-effect won't be affecting any other code?
> > 
> > Although most (not all) of bang methods are more efficient than their  
> > counterparts, I discourage the use of them unless the performance is  
> > really problem. "Premature optimization is the source of all evil".
> 
> Although I agree to the latter statement, I beg to differ on the general remark - at least a bit. I often use this
> 
> while ( line = gets )  
> line.chomp!  
> # work with line  
> end
> 
> because I know it's going to be potentially invoked often - even in scripts that are likely to run only on small input files. But for large files where not every line is processed it easily divides the number of instances created by a factor of 2.

Bad example:

while ( line = gets.chomp )  
&nbsp;&nbsp;&nbsp;# work with line  
end

> **···**
>
> > > In message "Re: destructive! operations" \>\> on Sun, 20 Feb 2005 17:22:34 +0900, Navindra Umanee \>\> \<navindra@cs.mcgill.ca\> writes:

---

<div class="post-metadata">

**Author:** ![Alexander\_Kellett2](https://avatars.discourse-cdn.com/v4/letter/a/ba8739/32.png) [@Alexander\_Kellett2](https://rubytalk.org/u/Alexander_Kellett2)\
**Post date:** [20 February 2005 16:54 UTC](https://rubytalk.org/t/destructive-operations/16625/5 "2005-02-20T16:54:08Z")

</div>

unless ruby cow's the string and  
the copy just has a truncating length,  
then this requires copying of the line.

Alex

> **···**
>
> On Feb 20, 2005, at 5:23 PM, Caio Tiago Oliveira wrote:
> 
> > Bad example:
> > 
> > while ( line = gets.chomp )  
> > &nbsp;&nbsp;# work with line  
> > end

---

<div class="post-metadata">

**Author:** ![James\_Edward\_Gray\_II](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@James\_Edward\_Gray\_II](https://rubytalk.org/u/James_Edward_Gray_II)\
**Post date:** [20 February 2005 17:16 UTC](https://rubytalk.org/t/destructive-operations/16625/6 "2005-02-20T17:16:47Z")

</div>

gets() returns a String or nil. nil does not support chomp(). When the chomp() is inside the while loop, this isn't an issue.

James Edward Gray II

> **···**
>
> On Feb 20, 2005, at 10:23 AM, Caio Tiago Oliveira wrote:
> 
> > Bad example:
> > 
> > while ( line = gets.chomp )  
> > &nbsp;&nbsp;# work with line  
> > end

---

<div class="post-metadata">

**Author:** ![Navindra\_Umanee](https://avatars.discourse-cdn.com/v4/letter/n/d78d45/32.png) [@Navindra\_Umanee](https://rubytalk.org/u/Navindra_Umanee)\
**Post date:** [20 February 2005 21:07 UTC](https://rubytalk.org/t/destructive-operations/16625/7 "2005-02-20T21:07:58Z")

</div>

Often think it would be nice if "" and 0 were treated like nil. Such  
functions could then return "". Heck, NilClass.to\_s and NilClass.to\_i  
already return "" and 0 respectively.

Matz talks about premature optimizations. It looks like nil was made  
this way for efficiency purposes! Can't say if it was premature  
though. 🙂

Thanks for the answers!

Cheers,  
Navin.

> **···**
>
> James Edward Gray II \<james@grayproductions.net\> wrote:
> 
> > \> Bad example:  
> > \>  
> > \> while ( line = gets.chomp )  
> > \> # work with line  
> > \> end
> > 
> > gets() returns a String or nil. nil does not support chomp(). When  
> > the chomp() is inside the while loop, this isn't an issue.

---

<div class="post-metadata">

**Author:** ![Navindra\_Umanee](https://avatars.discourse-cdn.com/v4/letter/n/d78d45/32.png) [@Navindra\_Umanee](https://rubytalk.org/u/Navindra_Umanee)\
**Post date:** [20 February 2005 21:10 UTC](https://rubytalk.org/t/destructive-operations/16625/8 "2005-02-20T21:10:57Z")

</div>

"false" too..

Incidentally I tried to make "", 0 and false respond positively to  
nil?. While they did, none of the boolean tests looked at nil? to  
make their decision. I guess that's the optimized part.

Cheers,  
Navin.

> **···**
>
> Navindra Umanee \<navindra@cs.mcgill.ca\> wrote:
> 
> > Often think it would be nice if "" and 0 were treated like nil. Such  
> > functions could then return "". Heck, NilClass.to\_s and NilClass.to\_i  
> > already return "" and 0 respectively.

---

<div class="post-metadata">

**Author:** ![Mark10](https://avatars.discourse-cdn.com/v4/letter/m/df705f/32.png) [@Mark10](https://rubytalk.org/u/Mark10)\
**Post date:** [20 February 2005 21:49 UTC](https://rubytalk.org/t/destructive-operations/16625/9 "2005-02-20T21:49:04Z")

</div>

Navindra Umanee wrote:

> **···**
>
> > James Edward Gray II \<james@grayproductions.net\> wrote:
> > 
> > > > Bad example:
> > > > 
> > > > while ( line = gets.chomp )  
> > > > # work with line  
> > > > end
> > > 
> > > gets() returns a String or nil. nil does not support chomp(). When the chomp() is inside the while loop, this isn't an issue.
> > 
> > Often think it would be nice if "" and 0 were treated like nil. Such  
> > functions could then return "".
> 
> One problem with this suggestion that comes to mind is, what if there are any blank lines in the file. then gets.chomp would return "" which would cause the while loop to exit. Probably not what you want
> 
> --  
> Mark Sparshatt

---

<div class="post-metadata">

**Author:** ![Csaba\_Henk4](https://avatars.discourse-cdn.com/v4/letter/c/9f8e36/32.png) [@Csaba\_Henk4](https://rubytalk.org/u/Csaba_Henk4)\
**Post date:** [20 February 2005 23:44 UTC](https://rubytalk.org/t/destructive-operations/16625/10 "2005-02-20T23:44:41Z")

</div>

Now, only false and nil drop you to the "else" branch of a conditional.  
Certainly, there are cases when it' not the best behaviour, and you  
have to write extra code to get the proper control flow when you test  
against 0 and/or "".

If it were the other way around, and "", 0 (and , {}) would lead to  
the "else" branch as well, again, there would occur situations when it's  
not the best behaviour and you'd need to write extra code to get things  
right.

Now the question is: which of the two behaviours is the right thing more  
often?

My experience is that the present way is the more optimal.

It's more cleaner as well. There is no intrinsic difference between ""  
and "a", 0 and 1, and [1]. It would be artificial to force an  
instance-sensitive policy when testing agains Strings, Integers and  
Arrays.

Those guys who make a difference when using them in a conditional are  
clearly separated into their respective cages (I mean, classes). That's  
nice...

Csaba

> **···**
>
> On 2005-02-20, Navindra Umanee \<navindra@cs.mcgill.ca\> wrote:
> 
> > Often think it would be nice if "" and 0 were treated like nil. Such  
> > functions could then return "". Heck, NilClass.to\_s and NilClass.to\_i  
> > already return "" and 0 respectively.

---

<div class="post-metadata">

**Author:** ![Austin\_Ziegler5](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/austin_ziegler5/32/1985_2.png) [@Austin\_Ziegler5](https://rubytalk.org/u/Austin_Ziegler5)\
**Post date:** [21 February 2005 14:41 UTC](https://rubytalk.org/t/destructive-operations/16625/11 "2005-02-21T14:41:13Z")

</div>

I'm far happier that they aren't.

-austin

> **···**
>
> On Mon, 21 Feb 2005 06:07:58 +0900, Navindra Umanee \<navindra@cs.mcgill.ca\> wrote:
> 
> > Often think it would be nice if "" and 0 were treated like nil. Such  
> > functions could then return "". Heck, NilClass.to\_s and NilClass.to\_i  
> > already return "" and 0 respectively.
> 
> --  
> Austin Ziegler \* halostatue@gmail.com  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\* Alternate: austin@halostatue.ca

---

<div class="post-metadata">

**Author:** ![Navindra\_Umanee](https://avatars.discourse-cdn.com/v4/letter/n/d78d45/32.png) [@Navindra\_Umanee](https://rubytalk.org/u/Navindra_Umanee)\
**Post date:** [20 February 2005 22:06 UTC](https://rubytalk.org/t/destructive-operations/16625/12 "2005-02-20T22:06:15Z")

</div>

Point. The chomp would make it not work here. There are many  
situations where it could be elegant though.

Cheers,  
Navin.

> **···**
>
> mark sparshatt \<msparshatt@yahoo.co.uk\> wrote:
> 
> > One problem with this suggestion that comes to mind is, what if there  
> > are any blank lines in the file. then gets.chomp would return "" which  
> > would cause the while loop to exit. Probably not what you want

---

<div class="post-metadata">

**Author:** ![Zach\_Dennis1](https://avatars.discourse-cdn.com/v4/letter/z/ea5d25/32.png) [@Zach\_Dennis1](https://rubytalk.org/u/Zach_Dennis1)\
**Post date:** [21 February 2005 14:51 UTC](https://rubytalk.org/t/destructive-operations/16625/13 "2005-02-21T14:51:01Z")

</div>

Austin Ziegler wrote:

> **···**
>
> > On Mon, 21 Feb 2005 06:07:58 +0900, Navindra Umanee \> \<navindra@cs.mcgill.ca\> wrote:
> > 
> > > Often think it would be nice if "" and 0 were treated like nil. Such  
> > > functions could then return "". Heck, NilClass.to\_s and NilClass.to\_i  
> > > already return "" and 0 respectively.
> > 
> > I'm far happier that they aren't.
> 
> +=1
> 
> Zach

---

<div class="post-metadata">

**Author:** ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://rubytalk.org/u/Robert)\
**Post date:** [21 February 2005 14:54 UTC](https://rubytalk.org/t/destructive-operations/16625/14 "2005-02-21T14:54:42Z")

</div>

"Austin Ziegler" \<halostatue@gmail.com\> schrieb im Newsbeitrag  
news:9e7db91105022106401bd2e5e@mail.gmail.com...

> **···**
>
> > On Mon, 21 Feb 2005 06:07:58 +0900, Navindra Umanee \> \<navindra@cs.mcgill.ca\> wrote:  
> > \> Often think it would be nice if "" and 0 were treated like nil. Such  
> > \> functions could then return "". Heck, NilClass.to\_s and NilClass.to\_i  
> > \> already return "" and 0 respectively.
> > 
> > I'm far happier that they aren't.
> 
> I didn't miss this "feature" either. I suspect that an unfinished  
> transition from Perl to Ruby makes people voice such whishes... 🙂
> 
> Kind regards
> 
> &nbsp;&nbsp;&nbsp;&nbsp;robert

---

<div class="post-metadata">

**Author:** ![Brian\_Schroder1](https://avatars.discourse-cdn.com/v4/letter/b/2bfe46/32.png) [@Brian\_Schroder1](https://rubytalk.org/u/Brian_Schroder1)\
**Post date:** [21 February 2005 15:10 UTC](https://rubytalk.org/t/destructive-operations/16625/15 "2005-02-21T15:10:53Z")

</div>

+1

but I think the votes don't matter, as it seems matz is quite firm on this one.

regards,

Brian

> **···**
>
> On Mon, 21 Feb 2005 23:41:13 +0900, Austin Ziegler \<halostatue@gmail.com\> wrote:
> 
> > On Mon, 21 Feb 2005 06:07:58 +0900, Navindra Umanee \> \<navindra@cs.mcgill.ca\> wrote:  
> > \> Often think it would be nice if "" and 0 were treated like nil. Such  
> > \> functions could then return "". Heck, NilClass.to\_s and NilClass.to\_i  
> > \> already return "" and 0 respectively.
> > 
> > I'm far happier that they aren't.

---

<div class="post-metadata">

**Author:** ![Christian\_Neukirche1](https://avatars.discourse-cdn.com/v4/letter/c/ee7513/32.png) [@Christian\_Neukirche1](https://rubytalk.org/u/Christian_Neukirche1)\
**Post date:** [21 February 2005 15:14 UTC](https://rubytalk.org/t/destructive-operations/16625/16 "2005-02-21T15:14:41Z")

</div>

Austin Ziegler \<halostatue@gmail.com\> writes:

> > Often think it would be nice if "" and 0 were treated like nil. Such  
> > functions could then return "". Heck, NilClass.to\_s and NilClass.to\_i  
> > already return "" and 0 respectively.
> 
> I'm far happier that they aren't.
> 
> -austin

Sometimes, I'd like an "eating nil" that returns itself for each  
method call, and is false.

Then, stuff like that would be possible

while line = gets.ignore\_if\_nil.chomp  
&nbsp;&nbsp;...  
end

Maybe just a crazy idea... 🙂

> **···**
>
> > On Mon, 21 Feb 2005 06:07:58 +0900, Navindra Umanee \> \<navindra@cs.mcgill.ca\> wrote:
> 
> --  
> Christian Neukirchen \<chneukirchen@gmail.com\> [http://chneukirchen.org](http://chneukirchen.org)

---

<div class="post-metadata">

**Author:** ![Navindra\_Umanee](https://avatars.discourse-cdn.com/v4/letter/n/d78d45/32.png) [@Navindra\_Umanee](https://rubytalk.org/u/Navindra_Umanee)\
**Post date:** [21 February 2005 15:31 UTC](https://rubytalk.org/t/destructive-operations/16625/17 "2005-02-21T15:31:42Z")

</div>

Maybe what is more annoying, is this type of inconsistency:

irb(main):003:0\> list[4..5]  
=\> nil  
irb(main):004:0\> list[0..3]  
=\>

Ruby is riddled with these and IMHO it tends to make code that much  
more ugly.

Cheers,  
Navin.

> **···**
>
> Austin Ziegler \<halostatue@gmail.com\> wrote:
> 
> > On Mon, 21 Feb 2005 06:07:58 +0900, Navindra Umanee \> \<navindra@cs.mcgill.ca\> wrote:  
> > \> Often think it would be nice if "" and 0 were treated like nil. Such  
> > \> functions could then return "". Heck, NilClass.to\_s and NilClass.to\_i  
> > \> already return "" and 0 respectively.
> > 
> > I'm far happier that they aren't.

---

<div class="post-metadata">

**Author:** ![Austin\_Ziegler5](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/austin_ziegler5/32/1985_2.png) [@Austin\_Ziegler5](https://rubytalk.org/u/Austin_Ziegler5)\
**Post date:** [21 February 2005 15:04 UTC](https://rubytalk.org/t/destructive-operations/16625/18 "2005-02-21T15:04:16Z")

</div>

Or C++/Java to Ruby (not the "", but definitely the 0). I \*have\* been  
bitten by 0/false problems in translations, but very few.

-austin

> **···**
>
> On Mon, 21 Feb 2005 23:54:42 +0900, Robert Klemme \<bob.news@gmx.net\> wrote:
> 
> > "Austin Ziegler" \<halostatue@gmail.com \> schrieb im Newsbeitrag  
> > news:9e7db91105022106401bd2e5e@mail.gmail.com...  
> > \> On Mon, 21 Feb 2005 06:07:58 +0900, Navindra Umanee \> \> \<navindra@cs.mcgill.ca \> wrote:  
> > \> \> Often think it would be nice if "" and 0 were treated like nil. Such  
> > \> \> functions could then return "". Heck, NilClass.to\_s and NilClass.to\_i  
> > \> \> already return "" and 0 respectively.  
> > \> I'm far happier that they aren't.  
> > I didn't miss this "feature" either. I suspect that an unfinished  
> > transition from Perl to Ruby makes people voice such whishes... 🙂
> 
> --  
> Austin Ziegler \* halostatue@gmail.com  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\* Alternate: austin@halostatue.ca

---

<div class="post-metadata">

**Author:** ![Brian\_Schroder1](https://avatars.discourse-cdn.com/v4/letter/b/2bfe46/32.png) [@Brian\_Schroder1](https://rubytalk.org/u/Brian_Schroder1)\
**Post date:** [21 February 2005 15:24 UTC](https://rubytalk.org/t/destructive-operations/16625/19 "2005-02-21T15:24:52Z")

</div>

What would the ignore\_if\_nil call in your example do? Wouldn't the  
above be equivalent to

while line = gets.chomp  
&nbsp;&nbsp;...  
end

Regards,

Brian

> **···**
>
> On Tue, 22 Feb 2005 00:14:41 +0900, Christian Neukirchen \<chneukirchen@gmail.com\> wrote:
> 
> > Austin Ziegler \<halostatue@gmail.com\> writes:
> > 
> > \> On Mon, 21 Feb 2005 06:07:58 +0900, Navindra Umanee \> \> \<navindra@cs.mcgill.ca\> wrote:  
> > \>\> Often think it would be nice if "" and 0 were treated like nil. Such  
> > \>\> functions could then return "". Heck, NilClass.to\_s and NilClass.to\_i  
> > \>\> already return "" and 0 respectively.  
> > \>  
> > \> I'm far happier that they aren't.  
> > \>  
> > \> -austin
> > 
> > Sometimes, I'd like an "eating nil" that returns itself for each  
> > method call, and is false.
> > 
> > Then, stuff like that would be possible
> > 
> > while line = gets.ignore\_if\_nil.chomp  
> > &nbsp;&nbsp;...  
> > end
> > 
> > Maybe just a crazy idea... 🙂

---

<div class="post-metadata">

**Author:** ![Bill\_Kelly](https://avatars.discourse-cdn.com/v4/letter/b/cc9497/32.png) [@Bill\_Kelly](https://rubytalk.org/u/Bill_Kelly)\
**Post date:** [21 February 2005 15:27 UTC](https://rubytalk.org/t/destructive-operations/16625/20 "2005-02-21T15:27:59Z")

</div>

> Sometimes, I'd like an "eating nil" that returns itself for each  
> method call, and is false.
> 
> Then, stuff like that would be possible
> 
> while line = gets.ignore\_if\_nil.chomp  
> &nbsp;&nbsp;...  
> end
> 
> Maybe just a crazy idea... 🙂

class NilClass  
&nbsp;&nbsp;def method\_missing(meth\_id, \*args); self; end  
end

?

🙂

ObjectiveC's nil works this way.

The downside is you don't find out as early when you  
received an unexpected nil, in situations where you  
would have preferred to be notified.

Regards,

Bill

> **···**
>
> From: "Christian Neukirchen" \<chneukirchen@gmail.com\>

[Next page](https://rubytalk.org/t/destructive-operations/16625.md?page=2)
