Reputation: 167
I nearly had this challenge on Code Wars in the bag but, I blew it because my knowledge of gsub
is sub-par at best. While I roughly understand the concept of gsub
, I would like a more thorough understanding of it (different ways you can use it could be helpful to my development) as well as a bit by bit explanation of the code below.
def autocorrect(input)
input.gsub(/\b(you+|u)\b/i, 'your sister')
end
Upvotes: 0
Views: 97
Reputation: 4561
You're taking any string that contains a match to the regular expression shown and replacing it with the second parameter which is in this case, "your sister". Regular expressions are a bit tricky in Ruby but essentially that regular expression is saying:
/ #starts the reg exp
\b #any word boundary
(you+|u) #the word 'you' with one or more of the letter 'u' added after it (so youuuuu would fit) or just the letter 'u' alone with a 'y' or 'o'... the pipe symbol is an or statement in reg-exp. taking one or the other for a match.
\b #again finishing a word boundary
/ #closes the expression.
Checkout Rubular for tips. http://rubular.com/
Upvotes: 1