Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Ruby script to convert CamelCase and snake_case each other.
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
You can’t perform that action at this time.