Created
October 28, 2016 08:48
-
-
Save komasaru/b3f22d5bcb8555deea1707b84d294045 to your computer and use it in GitHub Desktop.
Ruby script to convert CamelCase and snake_case each other.
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 to_camel | |
self.split(/_/).map(&:capitalize).join | |
# or | |
#self.split(/_/).map{ |w| w[0] = w[0].upcase; w }.join | |
end | |
def to_snake | |
self.gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2') | |
.gsub(/([a-z\d])([A-Z])/, '\1_\2') | |
.downcase | |
end | |
end | |
src = "CamlCase1ASnakeCase2B" | |
puts " SRC: #{src}" | |
snake = "CamlCase1ASnakeCase2B".to_snake | |
puts "SNAKE: #{snake}" | |
camel = snake.to_camel | |
puts "CAMEL: #{camel}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment