Skip to content

Instantly share code, notes, and snippets.

@francois-blanchard
Last active August 29, 2015 14:01
Show Gist options
  • Save francois-blanchard/31282920506e2096a0d9 to your computer and use it in GitHub Desktop.
Save francois-blanchard/31282920506e2096a0d9 to your computer and use it in GitHub Desktop.
Transform String (CamelCase) to symbol ruby
string = "nameLolApiTruc"
# "nameLolApiTruc"
string!.gsub(/([a-z\d])([A-Z])/,'\1_\2')
# "name_Lol_Api_Truc"
string.downcase.to_sym
# :name_lol_api_truc
# methods examples extend String and Hash class
class String
def to_symbol
self.gsub(/([a-z\d])([A-Z])/,'\1_\2').downcase.to_sym
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment