Class: Hash

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.map(obj, &block) ⇒ Object

map any Enumerable into a Hash, like Hash[obj.map … ]



6
7
8
9
10
11
12
# File 'lib/theusual/hash.rb', line 6

def map(obj, &block)
  Hash[
    obj.map do |*args|
      block.call *args
    end.compact
  ]
end

Instance Method Details

#-(other) ⇒ Object

set like operator

Raises:

  • (TypeError)


145
146
147
148
# File 'lib/theusual/hash.rb', line 145

def -(other)
  raise TypeError unless other.class <= Hash
  select {|k,v| !other.has_key? k}
end

#compact(modifier = nil) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/theusual/hash.rb', line 56

def compact(modifier = nil)
  falsy = modifier == :falsy
  blanks = falsy || modifier == :blanks

  reject do |k, v|
    isblank = blanks && v.respond_to?(:empty?) && v.empty?
    isfalsy = falsy && (v == 0)

    !v || isblank || isfalsy
  end
end

#compact!(modifier = nil) ⇒ Object



68
69
70
# File 'lib/theusual/hash.rb', line 68

def compact!(modifier = nil)
  replace compact(modifier)
end

#except(*keys) ⇒ Object



31
32
33
# File 'lib/theusual/hash.rb', line 31

def except(*keys)
  clone.except! *keys
end

#except!(*keys) ⇒ Object



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

def except!(*keys)
  keys.each { |key| delete(key) }
  self
end

#hmap(&block) ⇒ Object

map the block’s results back to a hash



74
75
76
# File 'lib/theusual/hash.rb', line 74

def hmap(&block)
  Hash[ map {|k, v| block.call(k, v) }.compact ]
end

#hmap!(&block) ⇒ Object



78
79
80
# File 'lib/theusual/hash.rb', line 78

def hmap!(&block)
  replace hmap &block
end

#kmap(&block) ⇒ Object

map keys, but preserve associated values ie. apidock.com/rails/v4.2.7/Hash/transform_keys



85
86
87
88
89
# File 'lib/theusual/hash.rb', line 85

def kmap(&block)
  Hash[map do |k, v|
    [ block.arity == 1 ? block.call(k) : block.call(k, v), v ]
  end]
end

#kmap!(&block) ⇒ Object



91
92
93
# File 'lib/theusual/hash.rb', line 91

def kmap!(&block)
  replace kmap &block
end

#ksort(&block) ⇒ Object

sort by key values, for pretty printing



110
111
112
113
114
115
116
117
118
119
120
# File 'lib/theusual/hash.rb', line 110

def ksort(&block)
  Hash[
    sort_by do |k, v|
      if block
        yield k
      else
        k
      end
    end
  ]
end

#ksort!(&block) ⇒ Object



122
123
124
# File 'lib/theusual/hash.rb', line 122

def ksort!(&block)
  replace ksort &block
end

#select_keys(*keys) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/theusual/hash.rb', line 41

def select_keys(*keys)
  if keys.length == 1 and keys.first.class < Enumerable
    keys = keys.first
  end

  Hash.map keys do |k|
    [ k, self[k] ]
  end
end

#select_keys!(*keys) ⇒ Object



51
52
53
# File 'lib/theusual/hash.rb', line 51

def select_keys!(*keys)
  replace select_keys *keys
end

#symbolize_keysObject



151
152
153
# File 'lib/theusual/hash.rb', line 151

def symbolize_keys
  clone.symbolize_keys!
end

#symbolize_keys!Object



155
156
157
# File 'lib/theusual/hash.rb', line 155

def symbolize_keys!
  kmap! { |key| key.to_sym rescue key }
end

#update(*hashes) ⇒ Object

expand update() to accept multiple arguments eg. {}.update(1, 2)



19
20
21
# File 'lib/theusual/hash.rb', line 19

def update(*hashes)
  clone.update! *hashes
end

#update!(*hashes) ⇒ Object



23
24
25
26
27
28
# File 'lib/theusual/hash.rb', line 23

def update!(*hashes)
  hashes.each do |h|
    h.each {|k,v| self[k] = v}
  end
  self
end

#vmap(&block) ⇒ Object

map values, but preserve associated keys ie. apidock.com/rails/v4.2.7/Hash/transform_values



98
99
100
# File 'lib/theusual/hash.rb', line 98

def vmap(&block)
  clone.vmap! &block
end

#vmap!(&block) ⇒ Object



102
103
104
105
106
# File 'lib/theusual/hash.rb', line 102

def vmap!(&block)
  each do |k, v|
    self[k] = block.arity == 1 ? block.call(v) : block.call(k, v)
  end
end

#vsort(&block) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
# File 'lib/theusual/hash.rb', line 127

def vsort(&block)
  Hash[
    sort_by do |k, v|
      if block
        yield v
      else
        v
      end
    end
  ]
end

#vsort!(&block) ⇒ Object



139
140
141
# File 'lib/theusual/hash.rb', line 139

def vsort!(&block)
  replace vsort &block
end