Created
September 18, 2018 22:16
-
-
Save ForeverZer0/c3e742ec383c3d04666f82dfb0c395ec to your computer and use it in GitHub Desktop.
snake_case, camelCase, and PascalCase extension methods for Ruby's String class.
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 | |
def snake_case | |
gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2') | |
.gsub(/([a-z\d])([A-Z])/,'\1_\2') | |
.tr("-", "_") | |
.downcase | |
end | |
def camel_case | |
sub(/^(?:(?=\b|[A-Z_])|\w)/) { $&.downcase } | |
.gsub(/(?:_|(\/))([a-z\d]*)/) { "#{$1}#{$2.capitalize}" } | |
end | |
def pascal_case | |
sub(/^[a-z\d]*/) { $&.capitalize } | |
.gsub(/(?:_|(\/))([a-z\d]*)/) { "#{$1}#{$2.capitalize}" } | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment