Ruby Conveniences
Posted by Tieg Zaharia Thu, 02 Feb 2006 07:49:00 GMT
Unbeknownst to me, there�s a quick way of doing a match & replace on a string in Ruby using just the brackets: (page 588 in the Ruby Book)
a = "bla bla bla"a[/a/i]="A"a => "blA bla bla"Unfortunately it only does one replace instead of a global replace, but I don�t know enough about RegExp�s now to know if there�s a way to do a global replace with this?


It should be /a/ig, I believe, for global (i is ignore case, g is global)
I guess I’m wrong – ruby doesn’t seem to support the g option. That’s lame.
a.gsub(/a/,’A’)