Take the tour ×  I have been perusing the documentation for
String today, and I saw the :sub method, which I'd never noticed
before. I've been using :gsub and it appears that they are
essentially the same. Can anyone explain the difference to me?
Thanks!  share|improve this question add comment  2 Answers  up
vote 54 down vote accepted  The g stands for global, as in
replace globally (all):  In irb:  >> "hello".sub('l', '*') =>
"he*lo" >> "hello".gsub('l', '*') => "he**o" share|improve this
answer Yeah. I understand now. In my defense, I don't think this
was very obvious... Until now, that is. –  Ryanmt Jul 20 '11 at
22:50 I agree with you that it is not obvious! Java calls these
replace and replaceAll. But Ruby has its roots in Perl which
uses the g modifier. It's just one of those things. –  Ray Toal
Jul 21 '11 at 0:27 Fortunately, it is now obvious. I'll know in
the future. –  Ryanmt Jul 21 '11 at 0:39 add comment  The
difference is that sub only replaces the first occurrence of the
pattern specified, wher