Class: Hash

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
mrblib/hash.rb,
mrbgems/mruby-hash-ext/mrblib/hash.rb

Overview

Hash

ISO 15.2.13

Constant Summary

Constants included from Enumerable

Enumerable::NONE

Instance Method Summary collapse

Methods included from Enumerable

#all?, #any?, #chain, #chunk, #chunk_while, #collect, #count, #cycle, #detect, #drop, #drop_while, #each_cons, #each_entry, #each_slice, #each_with_index, #each_with_object, #entries, #filter_map, #find_all, #find_index, #first, #flat_map, #grep, #grep_v, #group_by, #hash, #include?, #inject, #lazy, #max, #max_by, #min, #min_by, #minmax, #minmax_by, #none?, #one?, #partition, #reverse_each, #sort, #sort_by, #sum, #take, #take_while, #tally, #uniq, #zip

Instance Method Details

#<(hash) ⇒ Object

call-seq:

hash < other -> true or false

Returns true if hash is subset of other.

h1 = {a:1, b:2}
h2 = {a:1, b:2, c:3}
h1 < h2    #=> true
h2 < h1    #=> false
h1 < h1    #=> false

Raises:

  • (TypeError)


224
225
226
227
228
229
# File 'mrbgems/mruby-hash-ext/mrblib/hash.rb', line 224

def <(hash)
  raise TypeError, "can't convert #{hash.class} to Hash" unless Hash === hash
  size < hash.size and all? {|key, val|
    hash.key?(key) and hash[key] == val
  }
end

#<=(hash) ⇒ Object

call-seq:

hash <= other -> true or false

Returns true if hash is subset of other or equals to other.

h1 = {a:1, b:2}
h2 = {a:1, b:2, c:3}
h1 <= h2   #=> true
h2 <= h1   #=> false
h1 <= h1   #=> true

Raises:

  • (TypeError)


244
245
246
247
248
249
# File 'mrbgems/mruby-hash-ext/mrblib/hash.rb', line 244

def <=(hash)
  raise TypeError, "can't convert #{hash.class} to Hash" unless Hash === hash
  size <= hash.size and all? {|key, val|
    hash.key?(key) and hash[key] == val
  }
end

#>(hash) ⇒ Object

call-seq:

hash > other -> true or false

Returns true if other is subset of hash.

h1 = {a:1, b:2}
h2 = {a:1, b:2, c:3}
h1 > h2    #=> false
h2 > h1    #=> true
h1 > h1    #=> false

Raises:

  • (TypeError)


264
265
266
267
268
269
# File 'mrbgems/mruby-hash-ext/mrblib/hash.rb', line 264

def >(hash)
  raise TypeError, "can't convert #{hash.class} to Hash" unless Hash === hash
  size > hash.size and hash.all? {|key, val|
    key?(key) and self[key] == val
  }
end

#>=(hash) ⇒ Object

call-seq:

hash >= other -> true or false

Returns true if other is subset of hash or equals to hash.

h1 = {a:1, b:2}
h2 = {a:1, b:2, c:3}
h1 >= h2   #=> false
h2 >= h1   #=> true
h1 >= h1   #=> true

Raises:

  • (TypeError)


284
285
286
287
288
289
# File 'mrbgems/mruby-hash-ext/mrblib/hash.rb', line 284

def >=(hash)
  raise TypeError, "can't convert #{hash.class} to Hash" unless Hash === hash
  size >= hash.size and hash.all? {|key, val|
    key?(key) and self[key] == val
  }
end

#compactObject

call-seq:

hsh.compact     -> new_hsh

Returns a new hash with the nil values/key pairs removed

h = { a: 1, b: false, c: nil }
h.compact     #=> { a: 1, b: false }
h             #=> { a: 1, b: false, c: nil }


69
70
71
72
73
# File 'mrbgems/mruby-hash-ext/mrblib/hash.rb', line 69

def compact
  h=self.dup
  h.__compact
  h
end

#compact!Object

call-seq:

hsh.compact!    -> hsh

Removes all nil values from the hash. Returns the hash. Returns nil if the hash does not contain nil values.

h = { a: 1, b: false, c: nil }
h.compact!     #=> { a: 1, b: false }


55
56
57
# File 'mrbgems/mruby-hash-ext/mrblib/hash.rb', line 55

def compact!
  self.__compact
end

#deconstruct_keys(_keys) ⇒ Object

call-seq:

hash.deconstruct_keys(keys) -> hash

Returns self. This method is called by pattern matching to deconstruct the hash for matching.

The keys argument is an array of keys the pattern expects, or nil for exact matching (when **nil is used).



273
274
275
# File 'mrblib/hash.rb', line 273

def deconstruct_keys(_keys)
  self
end

#delete(key, &block) ⇒ Object

call-seq:

hash.delete(key) -> value or nil
hash.delete(key) {|key| ... } -> object

Delete the element with the key key. Return the value of the element if key was found. Return nil if nothing was found. If a block is given, call the block with the value of the element.

ISO 15.2.13.4.8



26
27
28
29
30
31
# File 'mrblib/hash.rb', line 26

def delete(key, &block)
  if block && !self.has_key?(key)
    return block.call(key)
  end
  self.__delete(key)
end

#delete_if(&block) ⇒ Object

call-seq: hsh.delete_if {| key, value | block } -> hsh hsh.delete_if -> an_enumerator

Deletes every key-value pair from hsh for which block evaluates to true.

If no block is given, an enumerator is returned instead.

h = { "a" => 100, "b" => 200, "c" => 300 }
h.delete_if {|key, value| key >= "b" }   #=> {"a"=>100}


131
132
133
134
135
136
137
138
# File 'mrbgems/mruby-hash-ext/mrblib/hash.rb', line 131

def delete_if(&block)
  return to_enum(:delete_if) unless block

  self.each do |k, v|
    self.delete(k) if block.call(k, v)
  end
  self
end

#dig(idx, *args) ⇒ Object

call-seq:

hsh.dig(key,...)                 -> object

Extracts the nested value specified by the sequence of key objects by calling dig at each step, returning nil if any intermediate step is nil.



299
300
301
302
303
304
305
306
# File 'mrbgems/mruby-hash-ext/mrblib/hash.rb', line 299

def dig(idx,*args)
  n = self[idx]
  if args.size > 0
    n&.dig(*args)
  else
    n
  end
end

#each(&block) ⇒ Object Also known as: each_pair

call-seq:

hsh.each      {| key, value | block } -> hsh
hsh.each_pair {| key, value | block } -> hsh
hsh.each                              -> an_enumerator
hsh.each_pair                         -> an_enumerator

Calls the given block for each element of self and pass the key and value of each element.

If no block is given, an enumerator is returned instead.

h = { "a" => 100, "b" => 200 }
h.each {|key, value| puts "#{key} is #{value}" }

produces:

a is 100 b is 200

ISO 15.2.13.4.9



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

def each(&block)
  return to_enum(:each) unless block

  keys = self.keys
  vals = self.values
  len = self.size
  i = 0
  while i < len
    block.call([keys[i], vals[i]])
    i += 1
  end
  self
end

#each_key(&block) ⇒ Object

call-seq:

hsh.each_key {| key | block } -> hsh
hsh.each_key                  -> an_enumerator

Calls the given block for each element of self and pass the key of each element.

If no block is given, an enumerator is returned instead.

h = { "a" => 100, "b" => 200 }
h.each_key {|key| puts key }

produces:

a b

ISO 15.2.13.4.10



87
88
89
90
91
92
# File 'mrblib/hash.rb', line 87

def each_key(&block)
  return to_enum(:each_key) unless block

  self.keys.each {|k| block.call(k)}
  self
end

#each_value(&block) ⇒ Object

call-seq:

hsh.each_value {| value | block } -> self
hsh.each_value                    -> an_enumerator

Calls the given block with each value; returns self:

If no block is given, an enumerator is returned instead.

h = { "a" => 100, "b" => 200 } h.each_value {|value| puts value }

produces:

100 200

ISO 15.2.13.4.11



112
113
114
115
116
117
# File 'mrblib/hash.rb', line 112

def each_value(&block)
  return to_enum(:each_value) unless block

  self.values.each {|v| block.call(v)}
  self
end

#fetch(key, none = NONE, &block) ⇒ Object

call-seq: hsh.fetch(key [, default] ) -> obj hsh.fetch(key) {| key | block } -> obj

Returns a value from the hash for the given key. If the key can't be found, there are several options: With no other arguments, it will raise an KeyError exception; if default is given, then that will be returned; if the optional code block is specified, then that will be run and its result returned.

h = { "a" => 100, "b" => 200 }
h.fetch("a")                            #=> 100
h.fetch("z", "go fish")                 #=> "go fish"
h.fetch("z") { |el| "go fish, #{el}"}   #=> "go fish, z"

The following example shows that an exception is raised if the key is not found and a default value is not supplied.

h = { "a" => 100, "b" => 200 }
h.fetch("z")

produces:

prog.rb:2:in 'fetch': key not found (KeyError)
 from prog.rb:2


103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'mrbgems/mruby-hash-ext/mrblib/hash.rb', line 103

def fetch(key, none=NONE, &block)
  unless self.key?(key)
    if block
      block.call(key)
    elsif !NONE.equal?(none)
      none
    else
      raise KeyError, "Key not found: #{key.inspect}"
    end
  else
    self[key]
  end
end

#fetch_values(*keys, &block) ⇒ Object

call-seq:

hsh.fetch_values(key, ...)                 -> array
hsh.fetch_values(key, ...) { |key| block } -> array

Returns an array containing the values associated with the given keys but also raises KeyError when one of keys can't be found. Also see Hash#values_at and Hash#fetch.

h = { "cat" => "feline", "dog" => "canine", "cow" => "bovine" }

h.fetch_values("cow", "cat")                   #=> ["bovine", "feline"]
h.fetch_values("cow", "bird")                  # raises KeyError
h.fetch_values("cow", "bird") { |k| k.upcase } #=> ["bovine", "BIRD"]


400
401
402
403
404
# File 'mrbgems/mruby-hash-ext/mrblib/hash.rb', line 400

def fetch_values(*keys, &block)
  keys.map do |k|
    self.fetch(k, &block)
  end
end

#flatten(level = 1) ⇒ Object

call-seq: hash.flatten -> an_array hash.flatten(level) -> an_array

Returns a new array that is a one-dimensional flattening of this hash. That is, for every key or value that is an array, extract its elements into the new array. Unlike Array#flatten, this method does not flatten recursively by default. The optional level argument determines the level of recursion to flatten.

a =  {1=> "one", 2 => [2,"two"], 3 => "three"}
a.flatten    # => [1, "one", 2, [2, "two"], 3, "three"]
a.flatten(2) # => [1, "one", 2, 2, "two", 3, "three"]


156
157
158
# File 'mrbgems/mruby-hash-ext/mrblib/hash.rb', line 156

def flatten(level=1)
  self.to_a.flatten(level)
end

#invertObject

call-seq: hsh.invert -> new_hash

Returns a new hash created by using hsh's values as keys, and the keys as values.

h = { "n" => 100, "m" => 100, "y" => 300, "d" => 200, "a" => 0 }
h.invert   #=> {0=>"a", 100=>"m", 200=>"d", 300=>"y"}


171
172
173
174
175
# File 'mrbgems/mruby-hash-ext/mrblib/hash.rb', line 171

def invert
  h = self.class.new
  self.each {|k, v| h[v] = k }
  h
end

#keep_if(&block) ⇒ Object

call-seq: hsh.keep_if {| key, value | block } -> hsh hsh.keep_if -> an_enumerator

Deletes every key-value pair from hsh for which block evaluates to false.

If no block is given, an enumerator is returned instead.



188
189
190
191
192
193
194
195
196
197
# File 'mrbgems/mruby-hash-ext/mrblib/hash.rb', line 188

def keep_if(&block)
  return to_enum(:keep_if) unless block

  self.each do |k, v|
    unless block.call([k, v])
      self.delete(k)
    end
  end
  self
end

#merge(*others, &block) ⇒ Object

call-seq: hsh.merge(other_hash..) -> hsh hsh.merge(other_hash..){|key, oldval, newval| block} -> hsh

Returns the new Hash formed by merging each of other_hashes into a copy of self.

Each argument in other_hashes must be a Hash. Adds the contents of other_hash to hsh. If no block is specified, entries with duplicate keys are overwritten with the values from other_hash, otherwise the value of each duplicate key is determined by calling the block with the key, its value in hsh and its value in other_hash.

Example:

h = {foo: 0, bar: 1, baz: 2}
h1 = {bat: 3, bar: 4}
h2 = {bam: 5, bat:6}
h3 = h.merge(h1, h2) { |key, old_value, new_value| old_value + new_value }
h3 # => {:foo=>0, :bar=>5, :baz=>2, :bat=>9, :bam=>5}

ISO 15.2.13.4.22



142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'mrblib/hash.rb', line 142

def merge(*others, &block)
  h = self.dup
  return h.__merge(*others) unless block
  i=0; len=others.size
  while i<len
    other = others[i]
    i += 1
    raise TypeError, "Hash required (#{other.class} given)" unless Hash === other
    other.each_key {|k|
      h[k] = (self.has_key?(k))? block.call(k, self[k], other[k]): other[k]
    }
  end
  h
end

#merge!(*others, &block) ⇒ Object Also known as: update

call-seq: hsh.merge!(other_hash..) -> hsh hsh.merge!(other_hash..){|key, oldval, newval| block} -> hsh

Adds the contents of other_hash to hsh. If no block is specified, entries with duplicate keys are overwritten with the values from other_hash, otherwise the value of each duplicate key is determined by calling the block with the key, its value in hsh and its value in other_hash.

h1 = { "a" => 100, "b" => 200 }
h2 = { "b" => 254, "c" => 300 }
h1.merge!(h2)   #=> {"a"=>100, "b"=>254, "c"=>300}

h1 = { "a" => 100, "b" => 200 }
h2 = { "b" => 254, "c" => 300 }
h1.merge!(h2) { |key, v1, v2| v1 }
                #=> {"a"=>100, "b"=>200, "c"=>300}


28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'mrbgems/mruby-hash-ext/mrblib/hash.rb', line 28

def merge!(*others, &block)
  i = 0; len=others.size
  return self.__merge(*others) unless block
  while i<len
    other = others[i]
    i += 1
    raise TypeError, "Hash required (#{other.class} given)" unless Hash === other
    other.each_key {|k|
      self[k] = (self.has_key?(k))? block.call(k, self[k], other[k]): other[k]
    }
  end
  self
end

#reject(&block) ⇒ Object

call-seq: hsh.reject {|key, value| block} -> a_hash hsh.reject -> an_enumerator

Returns a new hash consisting of entries for which the block returns false.

If no block is given, an enumerator is returned instead.

h = { "a" => 100, "b" => 200, "c" => 300 }
h.reject {|k,v| k < "b"}  #=> {"b" => 200, "c" => 300}
h.reject {|k,v| v > 100}  #=> {"a" => 100}

1.8/1.9 Hash#reject returns Hash; ISO says nothing.



198
199
200
201
202
203
204
205
206
207
208
# File 'mrblib/hash.rb', line 198

def reject(&block)
  return to_enum(:reject) unless block

  h = {}
  self.each {|k,v|
    unless block.call([k, v])
      h[k] = v
    end
  }
  h
end

#reject!(&block) ⇒ Object

call-seq: hsh.reject! {| key, value | block } -> hsh or nil hsh.reject! -> an_enumerator

Equivalent to Hash#delete_if, but returns nil if no changes were made.

1.8/1.9 Hash#reject! returns Hash; ISO says nothing.



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'mrblib/hash.rb', line 167

def reject!(&block)
  return to_enum(:reject!) unless block

  keys = []
  self.each {|k,v|
    if block.call([k, v])
      keys.push(k)
    end
  }
  return nil if keys.size == 0
  keys.each {|k|
    self.delete(k)
  }
  self
end

#select(&block) ⇒ Object Also known as: filter

call-seq: hsh.select {|key, value| block} -> a_hash hsh.select -> an_enumerator

Returns a new hash consisting of entries for which the block returns true.

If no block is given, an enumerator is returned instead.

h = { "a" => 100, "b" => 200, "c" => 300 }
h.select {|k,v| k > "a"}  #=> {"b" => 200, "c" => 300}
h.select {|k,v| v < 200}  #=> {"a" => 100}

1.9 Hash#select returns Hash; ISO says nothing



251
252
253
254
255
256
257
258
259
260
261
# File 'mrblib/hash.rb', line 251

def select(&block)
  return to_enum(:select) unless block

  h = {}
  self.each {|k,v|
    if block.call([k, v])
      h[k] = v
    end
  }
  h
end

#select!(&block) ⇒ Object Also known as: filter!

call-seq: hsh.select! {| key, value | block } -> hsh or nil hsh.select! -> an_enumerator

Equivalent to Hash#keep_if, but returns nil if no changes were made.

1.9 Hash#select! returns Hash; ISO says nothing.



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'mrblib/hash.rb', line 220

def select!(&block)
  return to_enum(:select!) unless block

  keys = []
  self.each {|k,v|
    unless block.call([k, v])
      keys.push(k)
    end
  }
  return nil if keys.size == 0
  keys.each {|k|
    self.delete(k)
  }
  self
end

#to_hObject

call-seq: hsh.to_h -> hsh or new_hash

Returns self. If called on a subclass of Hash, converts the receiver to a Hash object.



207
208
209
# File 'mrbgems/mruby-hash-ext/mrblib/hash.rb', line 207

def to_h
  self
end

#to_procObject



381
382
383
# File 'mrbgems/mruby-hash-ext/mrblib/hash.rb', line 381

def to_proc
  ->x{self[x]}
end

#transform_keys(&block) ⇒ Object

call-seq:

hsh.transform_keys {|key| block } -> new_hash
hsh.transform_keys                -> an_enumerator

Returns a new hash, with the keys computed from running the block once for each key in the hash, and the values unchanged.

If no block is given, an enumerator is returned instead.



318
319
320
321
322
323
324
325
326
# File 'mrbgems/mruby-hash-ext/mrblib/hash.rb', line 318

def transform_keys(&block)
  return to_enum(:transform_keys) unless block
  hash = {}
  self.keys.each do |k|
    new_key = block.call(k)
    hash[new_key] = self[k]
  end
  hash
end

#transform_keys!(&block) ⇒ Object

call-seq:

hsh.transform_keys! {|key| block } -> hsh
hsh.transform_keys!                -> an_enumerator

Invokes the given block once for each key in hsh, replacing it with the new key returned by the block, and then returns hsh.

If no block is given, an enumerator is returned instead.



337
338
339
340
341
342
# File 'mrbgems/mruby-hash-ext/mrblib/hash.rb', line 337

def transform_keys!(&block)
  return to_enum(:transform_keys!) unless block
  hash = self.transform_keys(&block)
  self.replace(hash)
  self
end

#transform_values(&b) ⇒ Object

call-seq:

hsh.transform_values {|value| block } -> new_hash
hsh.transform_values                  -> an_enumerator

Returns a new hash with the results of running the block once for every value. This method does not change the keys.

If no block is given, an enumerator is returned instead.



354
355
356
357
358
359
360
361
# File 'mrbgems/mruby-hash-ext/mrblib/hash.rb', line 354

def transform_values(&b)
  return to_enum(:transform_values) unless block_given?
  hash = {}
  self.keys.each do |k|
    hash[k] = yield(self[k])
  end
  hash
end

#transform_values!(&b) ⇒ Object

call-seq:

hsh.transform_values! {|key| block } -> hsh
hsh.transform_values!                -> an_enumerator

Invokes the given block once for each value in the hash, replacing with the new value returned by the block, and then returns hsh.

If no block is given, an enumerator is returned instead.



373
374
375
376
377
378
379
# File 'mrbgems/mruby-hash-ext/mrblib/hash.rb', line 373

def transform_values!(&b)
  return to_enum(:transform_values!) unless block_given?
  self.keys.each do |k|
    self[k] = yield(self[k])
  end
  self
end