Created
September 5, 2013 17:14
-
-
Save robinsloan/6453194 to your computer and use it in GitHub Desktop.
Ruby gsub with block
This file contains hidden or 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
# http://batsov.com/articles/2013/08/30/using-gsub-with-a-block/?utm_source=rubyweekly&utm_medium=email | |
# num will be passed the string '12' | |
"Apollo 12".gsub(/\d+/) { |num| num.to_i.next } | |
# => "Apollo 13" | |
# string yielded to block is always entire match; can't do matched groups at |m1, m2| etc. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment