Last active
August 28, 2025 08:26
-
-
Save Integralist/9503099 to your computer and use it in GitHub Desktop.
Revisions
-
Integralist revised this gist
Mar 12, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -49,4 +49,4 @@ def symbolize(obj) obj end multi_hash = symbolize(multi_hash) -
Integralist revised this gist
Mar 12, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -14,7 +14,7 @@ end # None of the above solutions work with a multi-level hash # They only work on the first level: {:foo=>"bar", :level1=>{"level2"=>"baz"}} # The following two variations solve the problem in the same way multi_hash = { 'foo' => 'bar', 'level1' => { 'level2' => 'baz' } } -
Integralist revised this gist
Mar 12, 2014 . 2 changed files with 52 additions and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,52 @@ hash = { 'foo' => 'bar' } # Version 1 hash = Hash[hash.map { |k, v| [k.to_sym, v] }] # Version 2 hash = hash.reduce({}) do |memo, (k, v)| memo.tap { |m| m[k.to_sym] = v } end # Version 3 hash = hash.reduce({}) do |memo, (k, v)| memo.merge({ k.to_sym => v}) end # None of the above solutions work with a multi-level hash # They both only work on the first level: {:foo=>"bar", :level1=>{"level2"=>"baz"}} # The following two variations solve the problem in the same way multi_hash = { 'foo' => 'bar', 'level1' => { 'level2' => 'baz' } } # Modify `Object` class Object def deep_symbolize_keys return self.reduce({}) do |memo, (k, v)| memo.tap { |m| m[k.to_sym] = v.deep_symbolize_keys } end if self.is_a? Hash return self.reduce([]) do |memo, v| memo << v.deep_symbolize_keys; memo end if self.is_a? Array self end end multi_hash = multi_hash.deep_symbolize_keys # Standalone method def symbolize(obj) return obj.reduce({}) do |memo, (k, v)| memo.tap { |m| m[k.to_sym] = symbolize(v) } end if obj.is_a? Hash return obj.reduce([]) do |memo, v| memo << symbolize(v); memo end if obj.is_a? Array obj end multi_hash = symbolize(hash) 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 charactersOriginal file line number Diff line number Diff line change @@ -1 +0,0 @@ -
Integralist created this gist
Mar 12, 2014 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1 @@ Hash[hash.map { |k,v| [k.to_sym, v] }]