Skip to content

Instantly share code, notes, and snippets.

@Integralist
Last active August 28, 2025 08:26
Show Gist options
  • Select an option

  • Save Integralist/9503099 to your computer and use it in GitHub Desktop.

Select an option

Save Integralist/9503099 to your computer and use it in GitHub Desktop.

Revisions

  1. Integralist revised this gist Mar 12, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Hash Keys to Symbols.rb
    Original file line number Diff line number Diff line change
    @@ -49,4 +49,4 @@ def symbolize(obj)
    obj
    end

    multi_hash = symbolize(hash)
    multi_hash = symbolize(multi_hash)
  2. Integralist revised this gist Mar 12, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Hash Keys to Symbols.rb
    Original 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 both only work on the first level: {:foo=>"bar", :level1=>{"level2"=>"baz"}}
    # 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' } }
  3. Integralist revised this gist Mar 12, 2014. 2 changed files with 52 additions and 1 deletion.
    52 changes: 52 additions & 0 deletions Hash Keys to Symbols.rb
    Original 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)
    1 change: 0 additions & 1 deletion gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -1 +0,0 @@
    Hash[hash.map { |k,v| [k.to_sym, v] }]
  4. Integralist created this gist Mar 12, 2014.
    1 change: 1 addition & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    Hash[hash.map { |k,v| [k.to_sym, v] }]