From: Jens Wille Date: 2008-04-03T06:13:54+09:00 Subject: Re: confused by 'test'.gsub(/.*/,'x') Thomas Wieczorek [2008-04-02 22:59]: > On Wed, Apr 2, 2008 at 10:55 PM, Yossef Mendelssohn > wrote: >> On Apr 2, 3:35 pm, "Thomas Wieczorek" >> wrote: >>> .* matches NO and ALL characters, so gsub() substitutes >>> ''(empty)(=>'x') and and 'test'(=>'x') with x, so you get >>> 'xx' >> That sounds like an explanation why ''.gsub(/.*/, 'x') is 'x' >> more than why 'test'.gsub(/.*/, 'x') is 'xx'. It seems to me >> that the .* should match [empty string]test[empty string] just once. > Yeah, it is confusing me, but I agreed on that explanation with > myself, when I read it once here. I'd also expect 'x' instead of 'xx' can't explain it either, i'm afraid. but you can see what it does like so: irb> 'test'.gsub(/.*/) { |m| p m; 'x'} "test" "" =>"xx" as soon as you anchor the regexp at the beginning of the string it gives the expected result: irb> 'test'.gsub(/\A.*/) { |m| p m; 'x'} "test" =>"x" or just do: irb> 'test'.sub(/.*/) { |m| p m; 'x'} "test" =>"x" ;-) cheers jens