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?

Posted in  | 3 comments

Comments

  1. Tim Connor said 10 minutes later:

    It should be /a/ig, I believe, for global (i is ignore case, g is global)

  2. Tim Connor said 18 minutes later:

    I guess I’m wrong – ruby doesn’t seem to support the g option. That’s lame.

  3. PJ Hyett said 2 days later:

    a.gsub(/a/,’A’)

Comments are disabled