Skip to content

Instantly share code, notes, and snippets.

@neanias
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neanias/af84b5adc99566303686 to your computer and use it in GitHub Desktop.
Save neanias/af84b5adc99566303686 to your computer and use it in GitHub Desktop.
Object extension
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