-
-
Save epitron/3a669e0ae6e83e11836c to your computer and use it in GitHub Desktop.
Multi-replace problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
QUESTION 6: Write a function multi_gsub that performs multiple, simultaneous | |
search-and-replace operations on a string. | |
Example: | |
"Lorem Ipsum #9191".multi_gsub([[/[a-z]/i, '#'], [/#/, 'P']]) | |
=> "##### ##### P9191" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class String | |
def mgsub(opts) | |
u = Regexp.union(opts.keys) | |
gsub(u) do |m| | |
opts.find { |re, s| m =~ re }.last | |
end | |
end | |
end | |
p "Lorem Ipsum #9191".mgsub(/[a-z]/i => '#', /#/ => 'P') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment