# Favorite idiom for "keep doing this until it returns nil/false"

**URL:** <https://rubytalk.org/t/favorite-idiom-for-keep-doing-this-until-it-returns-nil-false/40880>\
**Category:** ruby-talk\
**Created:** [24 September 2007 19:30 UTC](https://rubytalk.org/t/favorite-idiom-for-keep-doing-this-until-it-returns-nil-false/40880 "2007-09-24T19:30:05Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gavin\_Kistner3](https://avatars.discourse-cdn.com/v4/letter/g/dfb087/32.png) [@Gavin\_Kistner3](https://rubytalk.org/u/Gavin_Kistner3)\
**Post date:** [24 September 2007 19:30 UTC](https://rubytalk.org/t/favorite-idiom-for-keep-doing-this-until-it-returns-nil-false/40880/1 "2007-09-24T19:30:05Z")

</div>

I want to keep running gsub! on a string until it returns nil. How do  
you prefer to do this?

true while str.gsub!( ... )

while str.gsub!( ... ); end

begin  
&nbsp;&nbsp;done = !str.gsub!( ... )  
end until done

begin  
&nbsp;&nbsp;made\_replacement = str.gsub!( ... )  
end while made\_replacement

...some other way?

---

<div class="post-metadata">

**Author:** ![Gaspard\_Bucher1](https://avatars.discourse-cdn.com/v4/letter/g/e9bcb4/32.png) [@Gaspard\_Bucher1](https://rubytalk.org/u/Gaspard_Bucher1)\
**Post date:** [24 September 2007 19:55 UTC](https://rubytalk.org/t/favorite-idiom-for-keep-doing-this-until-it-returns-nil-false/40880/2 "2007-09-24T19:55:52Z")

</div>

I would use:  
while str.gsub!( ... ); end

"block ... while" is a 'regretted' feature  
([http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/6768\](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/6768%5C)).

> **···**
>
> 2007/9/24, Phrogz \<phrogz@mac.com\>:
> 
> > I want to keep running gsub! on a string until it returns nil. How do  
> > you prefer to do this?
> > 
> > true while str.gsub!( ... )
> > 
> > while str.gsub!( ... ); end
> > 
> > begin  
> > &nbsp;&nbsp;done = !str.gsub!( ... )  
> > end until done
> > 
> > begin  
> > &nbsp;&nbsp;made\_replacement = str.gsub!( ... )  
> > end while made\_replacement
> > 
> > ...some other way?

---

<div class="post-metadata">

**Author:** ![W\_James](https://avatars.discourse-cdn.com/v4/letter/w/e274bd/32.png) [@W\_James](https://rubytalk.org/u/W_James)\
**Post date:** [24 September 2007 20:30 UTC](https://rubytalk.org/t/favorite-idiom-for-keep-doing-this-until-it-returns-nil-false/40880/3 "2007-09-24T20:30:06Z")

</div>

Proper devotion to symmetry leads us to the knowledge that the  
sacred bang character must always be paired.

until !str.gsub!( ... ); end

> **···**
>
> On Sep 24, 2:27 pm, Phrogz \<phr...@mac.com\> wrote:
> 
> > I want to keep running gsub! on a string until it returns nil. How do  
> > you prefer to do this?
> > 
> > true while str.gsub!( ... )
> > 
> > while str.gsub!( ... ); end
> > 
> > begin  
> > &nbsp;&nbsp;done = !str.gsub!( ... )  
> > end until done
> > 
> > begin  
> > &nbsp;&nbsp;made\_replacement = str.gsub!( ... )  
> > end while made\_replacement
> > 
> > ...some other way?

---

<div class="post-metadata">

**Author:** ![Morton\_Goldberg](https://avatars.discourse-cdn.com/v4/letter/m/7ea924/32.png) [@Morton\_Goldberg](https://rubytalk.org/u/Morton_Goldberg)\
**Post date:** [24 September 2007 20:56 UTC](https://rubytalk.org/t/favorite-idiom-for-keep-doing-this-until-it-returns-nil-false/40880/4 "2007-09-24T20:56:30Z")

</div>

loop { break if str.gsub!(...).nil? }

because it clearly expresses the idea of "keep running gsub! on a string until it returns nil".

Regards, Morton

> **···**
>
> On Sep 24, 2007, at 3:30 PM, Phrogz wrote:
> 
> > I want to keep running gsub! on a string until it returns nil. How do  
> > you prefer to do this?
> > 
> > true while str.gsub!( ... )
> > 
> > while str.gsub!( ... ); end
> > 
> > begin  
> > &nbsp;&nbsp;done = !str.gsub!( ... )  
> > end until done
> > 
> > begin  
> > &nbsp;&nbsp;made\_replacement = str.gsub!( ... )  
> > end while made\_replacement
> > 
> > ...some other way?

---

<div class="post-metadata">

**Author:** ![W\_James](https://avatars.discourse-cdn.com/v4/letter/w/e274bd/32.png) [@W\_James](https://rubytalk.org/u/W_James)\
**Post date:** [26 September 2007 09:10 UTC](https://rubytalk.org/t/favorite-idiom-for-keep-doing-this-until-it-returns-nil-false/40880/5 "2007-09-26T09:10:03Z")

</div>

Less prolix:

0 while str.gsub!( ... )

> **···**
>
> On Sep 24, 2:27 pm, Phrogz \<phr...@mac.com\> wrote:
> 
> > I want to keep running gsub! on a string until it returns nil. How do  
> > you prefer to do this?
> > 
> > true while str.gsub!( ... )

---

<div class="post-metadata">

**Author:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [26 September 2007 13:07 UTC](https://rubytalk.org/t/favorite-idiom-for-keep-doing-this-until-it-returns-nil-false/40880/6 "2007-09-26T13:07:38Z")

</div>

a = "bccccce"  
begin  
&nbsp;&nbsp;callcc{|@a|}  
&nbsp;&nbsp;a.gsub!(/bc/,"cb") or raise nil  
&nbsp;&nbsp;@a.call  
rescue  
&nbsp;&nbsp;puts a  
end

What else, threads maybe 😉  
Robert

> **···**
>
> On 9/24/07, Phrogz \<phrogz@mac.com\> wrote:
> 
> > ...some other way?
> 
> --  
> I'm an atheist and that's it. I believe there's nothing we can know  
> except that we should be kind to each other and do what we can for  
> other people.  
> -- Katharine Hepburn

---

<div class="post-metadata">

**Author:** ![a11](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/a11/32/8169_2.png) [@a11](https://rubytalk.org/u/a11)\
**Post date:** [26 September 2007 15:33 UTC](https://rubytalk.org/t/favorite-idiom-for-keep-doing-this-until-it-returns-nil-false/40880/7 "2007-09-26T15:33:50Z")

</div>

loop{ str.gsub!(re, repl) or break }

a @ [http://drawohara.com/](http://drawohara.com/)

> **···**
>
> On Sep 24, 2007, at 1:30 PM, Phrogz wrote:
> 
> > I want to keep running gsub! on a string until it returns nil. How do  
> > you prefer to do this?
> > 
> > true while str.gsub!( ... )
> > 
> > while str.gsub!( ... ); end
> > 
> > begin  
> > &nbsp;&nbsp;done = !str.gsub!( ... )  
> > end until done
> > 
> > begin  
> > &nbsp;&nbsp;made\_replacement = str.gsub!( ... )  
> > end while made\_replacement
> > 
> > ...some other way?
> 
> --  
> we can deny everything, except that we have the possibility of being better. simply reflect on that.  
> h.h. the 14th dalai lama

---

<div class="post-metadata">

**Author:** ![Terry\_Poulin](https://avatars.discourse-cdn.com/v4/letter/t/ccd318/32.png) [@Terry\_Poulin](https://rubytalk.org/u/Terry_Poulin)\
**Post date:** [26 September 2007 23:50 UTC](https://rubytalk.org/t/favorite-idiom-for-keep-doing-this-until-it-returns-nil-false/40880/8 "2007-09-26T23:50:13Z")

</div>

Phrogz wrote:

> I want to keep running gsub! on a string until it returns nil. How do  
> you prefer to do this?
> 
> true while str.gsub!( ... )
> 
> while str.gsub!( ... ); end
> 
> begin  
> &nbsp;&nbsp;done = !str.gsub!( ... )  
> end until done
> 
> begin  
> &nbsp;&nbsp;made\_replacement = str.gsub!( ... )  
> end while made\_replacement
> 
> ...some other way?

Having been \*gotten\* accustomed to the

while (!expr) {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;do\_this();  
}

way of writing things, I would probably use this in ruby:

until str.gsub!( ... ) end

Generally, I write what ever I feel works when I am testing. The try to refine  
it into as clear and concise a manor as I can before I finish the prototype.

TerryP.

> **···**
>
> --  
> &nbsp;&nbsp;&nbsp;&nbsp;  
> Email and shopping with the feelgood factor!  
> 55% of income to good causes. [http://www.ippimail.com](http://www.ippimail.com)

---

<div class="post-metadata">

**Author:** ![Gaspard\_Bucher1](https://avatars.discourse-cdn.com/v4/letter/g/e9bcb4/32.png) [@Gaspard\_Bucher1](https://rubytalk.org/u/Gaspard_Bucher1)\
**Post date:** [24 September 2007 19:57 UTC](https://rubytalk.org/t/favorite-idiom-for-keep-doing-this-until-it-returns-nil-false/40880/9 "2007-09-24T19:57:03Z")

</div>

> "block ... while" is a 'regretted' feature  
> ([http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/6768\](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/6768%5C)).

Sorry, I meant  
"begin ... end while" is a 'regretted' feature

Gaspard

---

<div class="post-metadata">

**Author:** ![David\_A\_Black1](https://avatars.discourse-cdn.com/v4/letter/d/bcef8e/32.png) [@David\_A\_Black1](https://rubytalk.org/u/David_A_Black1)\
**Post date:** [26 September 2007 09:18 UTC](https://rubytalk.org/t/favorite-idiom-for-keep-doing-this-until-it-returns-nil-false/40880/10 "2007-09-26T09:18:41Z")

</div>

Hi --

> **···**
>
> On Wed, 26 Sep 2007, William James wrote:
> 
> > On Sep 24, 2:27 pm, Phrogz \<phr...@mac.com\> wrote:
> > 
> > > I want to keep running gsub! on a string until it returns nil. How do  
> > > you prefer to do this?
> > > 
> > > true while str.gsub!( ... )
> > 
> > Less prolix:
> > 
> > 0 while str.gsub!( ... )
> 
> I wouldn't worry; the first version isn't exactly "War and Peace" 🙂  
> I dislike the throwaway nature of both of them anyway.
> 
> David
> 
> --  
> Upcoming training from Ruby Power and Light, LLC:  
> &nbsp;&nbsp;&nbsp;\* Intro to Ruby on Rails, Edison, NJ, October 23-26  
> &nbsp;&nbsp;&nbsp;\* Advancing with Rails, Edison, NJ, November 6-9  
> Both taught by David A. Black.  
> See [http://www.rubypal.com](http://www.rubypal.com) for more info!

---

<div class="post-metadata">

**Author:** ![W\_James](https://avatars.discourse-cdn.com/v4/letter/w/e274bd/32.png) [@W\_James](https://rubytalk.org/u/W_James)\
**Post date:** [26 September 2007 17:00 UTC](https://rubytalk.org/t/favorite-idiom-for-keep-doing-this-until-it-returns-nil-false/40880/11 "2007-09-26T17:00:15Z")

</div>

More terse:

"War & Peace"

> **···**
>
> On Sep 26, 4:18 am, "David A. Black" \<dbl...@rubypal.com\> wrote:
> 
> > I wouldn't worry; the first version isn't exactly "War and Peace"

---

<div class="post-metadata">

**Author:** ![MenTaLguY1](https://avatars.discourse-cdn.com/v4/letter/m/97f17d/32.png) [@MenTaLguY1](https://rubytalk.org/u/MenTaLguY1)\
**Post date:** [26 September 2007 19:03 UTC](https://rubytalk.org/t/favorite-idiom-for-keep-doing-this-until-it-returns-nil-false/40880/12 "2007-09-26T19:03:57Z")

</div>

nil while war && peace

-mental

> **···**
>
> On Thu, 27 Sep 2007 02:00:15 +0900, William James \<w\_a\_x\_man@yahoo.com\> wrote:
> 
> > On Sep 26, 4:18 am, "David A. Black" \<dbl...@rubypal.com\> wrote:
> > 
> > > I wouldn't worry; the first version isn't exactly "War and Peace"
> > 
> > More terse:
> > 
> > "War & Peace"
