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

P

Phrogz

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?
 
W

William James

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
 
M

Morton Goldberg

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
 
W

William James

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

David A. Black

Hi --

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:
* 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!
 
A

ara.t.howard

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{ str.gsub!(re, repl) or break }

a @ http://drawohara.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top