Underscore ruby strings
def underscore(string)
return string.to_s unless /[A-Z-]/.match?(string)
string
.gsub(/([A-Z]+)(?=[A-Z][a-z])|([a-z\d])(?=[A-Z])/) { ($1 || $2) << '_' }
.tr('-', '_')
.downcase
end
Code snippets in this post are covered by 0BSD License.