Created
November 6, 2012 20:50
-
-
Save timcharper/4027440 to your computer and use it in GitHub Desktop.
to_underscore implementation
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 | |
# ruby mutation methods have the expectation to return self if a mutation occurred, nil otherwise. (see http://www.ruby-doc.org/core-1.9.3/String.html#method-i-gsub-21) | |
def to_underscore! | |
g = gsub!(/(.)([A-Z])/,'\1_\2'); d = downcase! | |
g || d | |
end | |
def to_underscore | |
dup.tap { |s| s.to_underscore! } | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment