I occasionally find myself with gsub regexp that either eat too much, or work within a nesting, such that I need to run gsub more than once in order to get the desired end result. The code I end up writing tends to be of the form:
while foo.gsub!( /.../, ... ); end
Frankly, I really don't like that. It's like JS people who write things like:
for ( var walkerNode = this; walkerNode.parent; walkerNode = walkerNode.parent ){}
'abusing' the for loop construct to hold their logic.
Is there some other form in Ruby of "keep doing this until it returns nil" that is less...gross?
I occasionally find myself with gsub regexp that either eat too much, or work within a nesting, such that I need to run gsub more than once in order to get the desired end result. The code I end up writing tends to be of the form:
while foo.gsub!( /.../, ... ); end
Frankly, I really don't like that. It's like JS people who write things like:
for ( var walkerNode = this; walkerNode.parent; walkerNode = walkerNode.parent ){}
'abusing' the for loop construct to hold their logic.
Is there some other form in Ruby of "keep doing this until it returns nil" that is less...gross?
loop{break if foo.gsub!( /.../, ... ).nil?}
?
its more explicit and avoids the empty body, but ....
i don't like it either
I'm kinda curious, can we see an example of a regex where this was necessary?
···
On Sep 25, 2005, at 4:25 PM, Gavin Kistner wrote:
I occasionally find myself with gsub regexp that either eat too much, or work within a nesting, such that I need to run gsub more than once in order to get the desired end result. The code I end up writing tends to be of the form:
module Kernel
def complete
loop { break unless yield }
end
end
if __FILE__ == $0
digits = "3211"
complete { digits.gsub!(/(\d)\1+/) { |d| d[0, 1].to_i + 1 } }
p digits # => "4"
end
__END__
Though I do realize that's not what you asked.
James Edward Gray II
···
On Sep 25, 2005, at 3:25 PM, Gavin Kistner wrote:
I occasionally find myself with gsub regexp that either eat too much, or work within a nesting, such that I need to run gsub more than once in order to get the desired end result. The code I end up writing tends to be of the form:
while foo.gsub!( /.../, ... ); end
Frankly, I really don't like that. It's like JS people who write things like:
for ( var walkerNode = this; walkerNode.parent; walkerNode = walkerNode.parent ){}
'abusing' the for loop construct to hold their logic.
Is there some other form in Ruby of "keep doing this until it returns nil" that is less...gross?
I occasionally find myself with gsub regexp that either eat too much,
or work within a nesting, such that I need to run gsub more than once
in order to get the desired end result. The code I end up writing
tends to be of the form:
while foo.gsub!( /.../, ... ); end
Frankly, I really don't like that. It's like JS people who write
things like:
for ( var walkerNode = this; walkerNode.parent; walkerNode =
walkerNode.parent ){}
'abusing' the for loop construct to hold their logic.
Is there some other form in Ruby of "keep doing this until it returns
nil" that is less...gross?
Here's a pared-down example from this week's ruby quiz:
class Array; def random; self[ rand( self.length ) ]; end; end
class String
def variation
out = self.dup
while out.gsub!( /\(([^())?]+)\)(\?)?/ ){
( $2 && ( rand > 0.5 ) ) ? '' : $1.split( '|' ).random
}; end
out
end
end
q = "(How (much|many)|What) is (the (value|result) of)? 10 + 2?"
10.times{ puts q.variation }
#=> What is the value of 10 + 2?
#=> How many is 10 + 2?
#=> What is 10 + 2?
#=> How many is 10 + 2?
#=> What is the value of 10 + 2?
#=> What is 10 + 2?
#=> What is the result of 10 + 2?
···
On Sep 25, 2005, at 3:33 PM, Logan Capaldo wrote:
I occasionally find myself with gsub regexp that either eat too much, or work within a nesting, such that I need to run gsub more than once in order to get the desired end result. The code I end up writing tends to be of the form:
while foo.gsub!( /.../, ... ); end
I'm kinda curious, can we see an example of a regex where this was necessary?
Ooops! I overlooked that one. /me smacks himself So the attribution
should of course go to Yanagawa Kazuhisa (I never know which is the
surname and which the christian name with asian names.).