Thanks for advices, I didn't know about $~ containting arrays of 
results, add new 'substitute' method is probably the best solution.

> This solution is not very generalizable.  It only works as presented for
> cases where all the stuff you want to discard looks the same.
No, it's no less generalizable than $X stuff, use splats if You have 
different matches.
"John Smith".substitute{|*tokens| ...}

And yes the provided sample is unclear, there where actually no 
replacement, maybe it should be something like that:

    "John Smith".gsub /(.+)\s(.+)/ do |name, family|
      "#{name[0..0]}. #{family}"
    end


Here's the complete solution:

    class String
      def substitute(*args)
        gsub(*args){yield Regexp.last_match.captures}
      end

      def substitute!(*args)
        gsub!(*args){yield Regexp.last_match.captures}
      end
    end

Thanks for help!

-- 
Posted via http://www.ruby-forum.com/.