Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/cliutils/ext/Hash+Extensions.rb

Overview

Hash Class

Contains many convenient methods borrowed from Rails
http://api.rubyonrails.org/classes/Hash.html
======================================================

Instance Method Summary (collapse)

Instance Method Details

- (Object) deep_merge!(other_hash, &block)

Methods
====================================================
----------------------------------------------------
deep_merge! method

Deep merges a hash into the current one.
@param other_hash The hash to merge in
@param &block
@return Hash
----------------------------------------------------


19
20
21
22
23
24
25
26
27
28
29
# File 'lib/cliutils/ext/Hash+Extensions.rb', line 19

def deep_merge!(other_hash, &block)
  other_hash.each_pair do |k,v|
    tv = self[k]
    if tv.is_a?(Hash) && v.is_a?(Hash)
      self[k] = tv.deep_merge(v, &block)
    else
      self[k] = block && tv ? block.call(k, tv, v) : v
    end
  end
  self
end

- (Object) deep_stringify_keys


deep_stringify_keys method

Recursively turns all Hash keys into strings and
returns the new Hash.
@return Hash
----------------------------------------------------


38
39
40
# File 'lib/cliutils/ext/Hash+Extensions.rb', line 38

def deep_stringify_keys
  deep_transform_keys{ |key| key.to_s }
end

- (Object) deep_stringify_keys!


deep_symbolize_keys! method

Same as deep_stringify_keys, but destructively
alters the original Hash.
@return Hash
----------------------------------------------------


49
50
51
# File 'lib/cliutils/ext/Hash+Extensions.rb', line 49

def deep_stringify_keys!
  deep_transform_keys!{ |key| key.to_s }
end

- (Object) deep_symbolize_keys


deep_symbolize_keys method

Recursively turns all Hash keys into symbols and
returns the new Hash.
@return Hash
----------------------------------------------------


60
61
62
# File 'lib/cliutils/ext/Hash+Extensions.rb', line 60

def deep_symbolize_keys
  deep_transform_keys{ |key| key.to_sym rescue key }
end

- (Object) deep_symbolize_keys!


deep_symbolize_keys! method

Same as deep_symbolize_keys, but destructively
alters the original Hash.
@return Hash
----------------------------------------------------


71
72
73
# File 'lib/cliutils/ext/Hash+Extensions.rb', line 71

def deep_symbolize_keys!
  deep_transform_keys!{ |key| key.to_sym rescue key }
end

- (Object) deep_transform_keys(&block)


deep_transform_keys method

Generic method to perform recursive operations on a
Hash.
@return Hash
----------------------------------------------------


82
83
84
# File 'lib/cliutils/ext/Hash+Extensions.rb', line 82

def deep_transform_keys(&block)
  _deep_transform_keys_in_object(self, &block)
end

- (Object) deep_transform_keys!(&block)


deep_transform_keys! method

Same as deep_transform_keys, but destructively
alters the original Hash.
@return Hash
----------------------------------------------------


93
94
95
# File 'lib/cliutils/ext/Hash+Extensions.rb', line 93

def deep_transform_keys!(&block)
  _deep_transform_keys_in_object!(self, &block)
end