Last active
August 29, 2015 14:00
-
-
Save neanias/af84b5adc99566303686 to your computer and use it in GitHub Desktop.
Object extension
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 Object | |
def symbolize(target = nil) | |
target = self.clone if target == nil | |
if target.class == Hash | |
clone = target.clone | |
clone.each_pair do |key, value| | |
target[key] = value.symbolize! if [Array, Hash].include? value.class | |
target[(key.to_sym rescue key) || key] = target.delete(key) | |
end | |
elsif target.class == Array | |
target.each do |value| | |
value.symbolize! if [Array, Hash].include? value.class | |
end | |
end | |
target | |
end | |
def symbolize! | |
self.symbolize(self) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment