public
Last active

to_underscore implementation

  • Download Gist
string.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11
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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.