What is the difference between gsub and sub methods for Ruby Strings

Viewed 56213

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!

4 Answers

sub and gsub perform replacement of the first and all matches respectively.

sub(pattern, replacement, x, ignore.case = FALSE, perl = FALSE,
    fixed = FALSE, useBytes = FALSE)

gsub(pattern, replacement, x, ignore.case = FALSE, perl = FALSE,
     fixed = FALSE, useBytes = FALSE)


sub("4", "8", "An Introduction to R Software Course will be of 4 weeks duration" )  
##"An Introduction to R Software Course will be of 8 weeks duration"

gsub("4", "8", "An Introduction to R Software Course will be of 4 weeks duration" )
##"An Introduction to R Software Course will be of 8 weeks duration"
Related