Class: Hash

Inherits:
Object show all
Defined in:
lib/cuboid/ruby/hash.rb

Instance Method Summary collapse

Instance Method Details

#my_stringify_keys(recursively = true) ⇒ Hash

Converts the hash keys to strings.

Parameters:

  • recursively (Boolean) (defaults to: true)

    Go through the Hash recursively?

Returns:

  • (Hash)

    Hash with self‘s keys recursively converted to strings.



14
15
16
17
18
19
20
21
# File 'lib/cuboid/ruby/hash.rb', line 14

def my_stringify_keys( recursively = true )
    stringified = {}
    each do |k, v|
        stringified[k.to_s] = (recursively && v.is_a?( Hash ) ?
            v.my_stringify_keys : v)
    end
    stringified
end

#my_symbolize_keys(recursively = true) ⇒ Hash

Converts the hash keys to symbols.

Parameters:

  • recursively (Boolean) (defaults to: true)

    Go through the Hash recursively?

Returns:

  • (Hash)

    Hash with self‘s keys recursively converted to symbols.



30
31
32
33
34
35
36
37
38
39
# File 'lib/cuboid/ruby/hash.rb', line 30

def my_symbolize_keys( recursively = true )
    symbolize = {}
    each do |k, v|
        k = k.respond_to?(:to_sym) ? k.to_sym : k

        symbolize[k] = (recursively && v.is_a?( Hash ) ?
            v.my_symbolize_keys : v)
    end
    symbolize
end