Class: Lotus::Utils::Hash

Inherits:
Hash
  • Object
show all
Defined in:
lib/lotus/utils/hash.rb

Overview

Hash on steroids

Since:

  • 0.1.0

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ Hash

Initialize the hash

Parameters:

  • hash (::Hash, Hash) (defaults to: {})

    the value we want to use to initialize this instance

Since:

  • 0.1.0



13
14
15
# File 'lib/lotus/utils/hash.rb', line 13

def initialize(hash = {})
  merge! hash
end

Instance Method Details

#symbolize!Hash

Convert in-place all the keys to Symbol instances, nested hashes are converted too.

Examples:

require 'lotus/utils/hash'

hash = Lotus::Utils::Hash.new 'a' => 23, 'b' => { 'c' => ['x','y','z'] }
hash.symbolize!

hash.keys    # => [:a, :b]
hash.inspect # => {:a=>23, :b=>{:c=>["x", "y", "z"]}}

Returns:

Since:

  • 0.1.0



31
32
33
34
35
36
37
38
39
40
# File 'lib/lotus/utils/hash.rb', line 31

def symbolize!
  keys.each do |k|
    v = delete(k)
    v = Hash.new(v).symbolize! if v.is_a?(::Hash)

    self[k.to_sym] = v
  end

  self
end