Salida snakecase de Ruby Facets
Los siguientes casos son manejados, como se ve a continuación:
"SnakeCase".snakecase #=> "snake_case"
"Snake-Case".snakecase #=> "snake_case"
"Snake Case".snakecase #=> "snake_case"
"Snake - Case".snakecase #=> "snake_case"
Desde: https://github.com/rubyworks/facets/blob/master/lib/core/facets/string/snakecase.rb
class String
# Underscore a string such that camelcase, dashes and spaces are
# replaced by underscores. This is the reverse of {#camelcase},
# albeit not an exact inverse.
#
# "SnakeCase".snakecase #=> "snake_case"
# "Snake-Case".snakecase #=> "snake_case"
# "Snake Case".snakecase #=> "snake_case"
# "Snake - Case".snakecase #=> "snake_case"
#
# Note, this method no longer converts `::` to `/`, in that case
# use the {#pathize} method instead.
def snakecase
#gsub(/::/, '/').
gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
gsub(/([a-z\d])([A-Z])/,'\1_\2').
tr('-', '_').
gsub(/\s/, '_').
gsub(/__+/, '_').
downcase
end
#
alias_method :underscore, :snakecase
# TODO: Add *separators to #snakecase, like camelcase.
end
FYI: minúsculas y subrayado es "oficialmente" llamada serpiente case – Andrew
Es extraño ver "oficialmente" entre comillas, y sin embargo, esa es la mejor manera de describirlo. –