Class: Hash

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

Instance Method Summary collapse

Instance Method Details

#chunk(max_length = nil, &block) ⇒ Object



80
81
82
83
84
85
86
# File 'lib/ext/hash.rb', line 80

def chunk(max_length=nil, &block)
  if ::RUBY_VERSION >= "1.9" && block_given?
    super(&block)
  else
    each_slice(max_length).to_a.map! { |ary| ary.to_h }
  end
end

#deep_merge(second) ⇒ Object

Merges self with another second, recursively.

This code was lovingly stolen from some random gem: gemjack.com/gems/tartan-0.1.1/classes/Hash.html

Thanks to whoever made it.

Modified to provide same functionality with Arrays



11
12
13
14
15
# File 'lib/ext/hash.rb', line 11

def deep_merge(second)
  target = deep_dup
  target.deep_merge!(second.deep_dup)
  target
end

#deep_merge!(second) ⇒ Object

From: www.gemtacular.com/gemdocs/cerberus-0.2.2/doc/classes/Hash.html File lib/cerberus/utils.rb, line 42 Modified to provide same functionality with Arrays



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/ext/hash.rb', line 21

def deep_merge!(second)
  return nil unless second
  type_assert(second, Hash)
  second.each_pair do |k,v|
    if self[k].is_a?(Array) and second[k].is_a?(Array)
      self[k].deep_merge!(second[k])
    elsif self[k].is_a?(Hash) and second[k].is_a?(Hash)
      self[k].deep_merge!(second[k])
    else
      self[k] = second[k]
    end
  end
end

#expand_nested(key_joiner = "__") ⇒ Object



134
135
136
137
138
# File 'lib/ext/hash.rb', line 134

def expand_nested(key_joiner="__")
  target = deep_dup
  target.expand_nested!(key_joiner)
  target
end

#expand_nested!(key_joiner = "__") ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/ext/hash.rb', line 107

def expand_nested!(key_joiner="__")
  changed = false
  keys.each do |base_key|
    if base_key[key_joiner]
      nested_keys = base_key.split(key_joiner)
      hsh_ref = self
      nested_keys.each_with_index do |key,idx|
        if idx == nested_keys.size - 1
          hsh_ref[key] = self[base_key]
        else
          hsh_ref[key] ||= {}
          hsh_ref = hsh_ref[key]
        end
      end
      delete(base_key)
      changed = true
    end
  end
  changed ? self : nil
end

#flatten_nested(key_joiner = "__") ⇒ Object



128
129
130
131
132
# File 'lib/ext/hash.rb', line 128

def flatten_nested(key_joiner="__")
  target = deep_dup
  target.flatten_nested!(key_joiner)
  target
end

#flatten_nested!(key_joiner = "__") ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/ext/hash.rb', line 92

def flatten_nested!(key_joiner="__")
  changed = false
  keys.each do |base_key|
    if self[base_key].is_a?(Hash)
      self[base_key].flatten_nested!(key_joiner)
      self[base_key].each_pair do |k,v|
        self["#{base_key}#{key_joiner}#{k}"] = v
      end
      delete(base_key)
      changed = true
    end
  end
  changed ? self : nil
end

#not_empty?(arg) ⇒ Boolean

Returns:



88
89
90
# File 'lib/ext/hash.rb', line 88

def not_empty?(arg)
  !empty?
end

#reject_keys(other, &block) ⇒ Object Also known as: -



60
61
62
63
64
# File 'lib/ext/hash.rb', line 60

def reject_keys(other, &block)
  target = dup
  target.reject_keys!(other, &block)
  target
end

#reject_keys!(other, &block) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/ext/hash.rb', line 67

def reject_keys!(other, &block)
  unless block_given?
    type_assert(other, Array, Hash, Regexp)
    other = other.keys if Hash === other
    if Regexp === other
      block = proc { |k| other === k }
    else
      block = proc { |k| other.include?(k) }
    end
  end
  self.reject! { |key,val| block[key] }
end

#select_keys(other, &block) ⇒ Object Also known as: &



40
41
42
43
44
# File 'lib/ext/hash.rb', line 40

def select_keys(other, &block)
  target = dup
  target.select_keys!(other, &block)
  target
end

#select_keys!(other, &block) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/ext/hash.rb', line 47

def select_keys!(other, &block)
  unless block_given?
    type_assert(other, Array, Hash, Regexp)
    other = other.keys if Hash === other
    if Regexp === other
      block = proc { |k| other === k }
    else
      block = proc { |k| other.include?(k) }
    end
  end
  self.reject! { |key,val| !block[key] }
end

#to_hObject Also known as: to_hash



35
36
37
# File 'lib/ext/hash.rb', line 35

def to_h
  self
end