Skip to content

Instantly share code, notes, and snippets.

@timcharper
Created November 6, 2012 20:50
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save timcharper/4027440 to your computer and use it in GitHub Desktop.
Save timcharper/4027440 to your computer and use it in GitHub Desktop.
to_underscore implementation
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