On Wed, Apr 2, 2008 at 10:12 PM, Wybo Dekker <wybo / servalys.nl> wrote:
> Why do I get "xx" instead of "x" in the following:
>
>  $ irb
>  >> 'test'.gsub(/.*/,'x')
>  => "xx"
>

.* matches NO and ALL characters, so gsub() substitutes
''(empty)(=>'x') and and 'test'(=>'x') with x, so you get 'xx'

>  and even more confusing (to me):
>
>  >> "x\n".gsub(/.*/,'y')
>  => "yy\ny"
>

Same goes here as above. If you want to replace each character use
'test'.gsub(/./,'x') #=> 'xxxx'
or if you want to replace all characters in each line, use
"test\ntest".gsub(/.+/,'x') #=> "x\nx"