I was looking at the gsub method on the String.prototype object in Prototype. I was trying to determine the purpose of this method. It seems like it duplicates the functionality of the built-in replace() method. So:

"red, blue, green".gsub(/, /, ' - ');
"red, blue, green".replace(/, /g, ' - ');

What is the difference between these lines?

Also both functions allow the second parameter to be a function (although with different arguments).

Finally both arguments allow parenthetical matches to be in the replacement string (although one uses #{n} and the other uses $n).

My question is why does gsub exist? Seems that although gsub is more ruby-like it is not needed and the replace() method suffices. Am I wrong?

Eric

_______________________________________________
Rails-spinoffs mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs

Reply via email to