Last active
May 15, 2018 18:34
-
-
Save ssaunier/3866812 to your computer and use it in GitHub Desktop.
Recurively symbolize ruby object keys
This file contains hidden or 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
module Symbolizer | |
def self.deep_symbolize(object) | |
if object.kind_of? Hash | |
object.keys.each do |k| | |
object[k == "_id" ? :id : k.to_sym] = self.deep_symbolize(object.delete k) | |
end | |
object | |
elsif object.kind_of? Array | |
object.map { |e| self.deep_symbolize e } | |
else | |
object | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Little trick to convert mongoid
"_id"
to symbol:id
.