Last active
August 29, 2015 14:01
-
-
Save francois-blanchard/31282920506e2096a0d9 to your computer and use it in GitHub Desktop.
Transform String (CamelCase) to symbol ruby
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
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