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

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
  done = !str.gsub!( ... )
end until done

begin
  made_replacement = str.gsub!( ... )
end while made_replacement

...some other way?

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

···

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
  done = !str.gsub!( ... )
end until done

begin
  made_replacement = str.gsub!( ... )
end while made_replacement

...some other way?

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
  done = !str.gsub!( ... )
end until done

begin
  made_replacement = str.gsub!( ... )
end while made_replacement

...some other way?

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
  done = !str.gsub!( ... )
end until done

begin
  made_replacement = str.gsub!( ... )
end while made_replacement

...some other way?

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!( ... )

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

What else, threads maybe :wink:
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

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

a @ 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
  done = !str.gsub!( ... )
end until done

begin
  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

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
  done = !str.gsub!( ... )
end until done

begin
  made_replacement = str.gsub!( ... )
end while made_replacement

...some other way?

Having been *gotten* accustomed to the

while (!expr) {
        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.

···

--
    
Email and shopping with the feelgood factor!
55% of income to good causes. http://www.ippimail.com

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

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

Gaspard

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" :slight_smile:
I dislike the throwaway nature of both of them anyway.

David

--
Upcoming training from Ruby Power and Light, LLC:
   * Intro to Ruby on Rails, Edison, NJ, October 23-26
   * Advancing with Rails, Edison, NJ, November 6-9
Both taught by David A. Black.
See http://www.rubypal.com for more info!

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"

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"