Robert Klemme
3/24/2009 9:13:00 PM
On 24.03.2009 21:32, Raj Singh wrote:
> a = " Ruby's gem "
>
> puts a.gsub(/[']/, "") #=> 1. Rubys gem
>
> puts a.gsub(/[']/, "'") #=> 2. Ruby's gem
>
> puts a.gsub(/[']/, "\'") #=> 3. Ruby's gem
>
> puts a.gsub(/[']/, "\\'") #=> 4. Rubys gem s gem
>
> puts a.gsub(/[']/, "\\\'") #=> 5. Rubys gem s gem
>
> puts a.gsub(/[']/, "\\\\'") #=> 6. Ruby\'s gem
>
> puts " "
>
>
> I couldn't understand the result for #4 and #5. Any explanation is
> appreciated.
The sequence \' in a replacement string is interpreted as the post
match. That's why you see the duplication. You'll see this nicely by
bracketing the replacement:
irb(main):012:0> a = " Ruby's gem "
=> " Ruby's gem "
irb(main):013:0> a.gsub /'/, "<\\'>"
=> " Ruby<s gem >s gem "
There is also pre match:
irb(main):014:0> a.gsub /'/, "<\\`>"
=> " Ruby< Ruby>s gem "
Kind regards
robert
--
remember.guy do |as, often| as.you_can - without end