Class: Hash

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
mrblib/hash.rb,
mrblib/hash.rb,
src/hash.c

Overview

15.2.13

Instance Method Summary (collapse)

Methods included from Enumerable

#__sort_sub__, #all?, #any?, #collect, #detect, #each_with_index, #entries, #find_all, #grep, #hash, #include?, #inject, #max, #min, #partition, #sort

Instance Method Details

- (Object) ==(hash)

Equality—Two hashes are equal if they each contain the same number

of keys and if each key-value pair is equal to (according to
<code>Object#==</code>) the corresponding elements in the other
hash.

ISO 15.2.13.4.1



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'mrblib/hash.rb', line 13

def ==(hash)
  return true if self.equal?(hash)
  begin
    hash = hash.to_hash
  rescue NoMethodError
    return false
  end
  return false if self.size != hash.size
  self.each do |k,v|
    return false unless hash.key?(k)
    return false unless self[k] == hash[k]
  end
  return true
end

- (Object) __update(h)



345
346
347
348
# File 'mrblib/hash.rb', line 345

def __update(h)
  h.each_key{|k| self[k] = h[k]}
  self
end

- (Object) _inspect

internal method for Hash inspection



196
197
198
199
200
201
# File 'mrblib/hash.rb', line 196

def _inspect
  return "{}" if self.size == 0
  "{"+self.map {|k,v|
    k._inspect + "=>" + v._inspect
  }.join(", ")+"}"
end

- (Object) delete(key, &block)

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



56
57
58
59
60
61
62
# File 'mrblib/hash.rb', line 56

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

- (Object) each(&block)

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

call-seq:

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

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



86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'mrblib/hash.rb', line 86

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

  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

- (Object) each_key(&block)

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

call-seq:

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

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



119
120
121
122
123
124
# File 'mrblib/hash.rb', line 119

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

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

- (Object) each_value(&block)

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

call-seq:

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

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



145
146
147
148
149
150
# File 'mrblib/hash.rb', line 145

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

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

- (Boolean) eql?(hash)

Returns true if hash and other are both hashes with the same content compared by eql?.

ISO 15.2.13.4.32 (x)

Returns:

  • (Boolean)


33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'mrblib/hash.rb', line 33

def eql?(hash)
  return true if self.equal?(hash)
  begin
    hash = hash.to_hash
  rescue NoMethodError
    return false
  end
  return false if self.size != hash.size
  self.each do |k,v|
    return false unless hash.key?(k)
    return false unless self[k].eql?(hash[k])
  end
  return true
end

- (Object) inspect Also known as: to_s

ISO 15.2.13.4.30 (x)



206
207
208
209
210
211
212
# File 'mrblib/hash.rb', line 206

def inspect
  begin
    self._inspect
  rescue SystemStackError
    "{...}"
  end
end

- (Object) merge(other, &block)

Return a hash which contains the content of self and other. If a block is given it will be called for each element with a duplicate key. The value of the block will be the final value of this element.

ISO 15.2.13.4.22

Raises:



180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'mrblib/hash.rb', line 180

def merge(other, &block)
  h = {}
  raise TypeError, "can't convert argument into Hash" unless other.respond_to?(:to_hash)
  other = other.to_hash
  self.each_key{|k| h[k] = self[k]}
  if block
    other.each_key{|k|
      h[k] = (self.has_key?(k))? block.call(k, self[k], other[k]): other[k]
    }
  else
    other.each_key{|k| h[k] = other[k]}
  end
  h
end

- (Object) rehash

call-seq:

  hsh.rehash -> hsh

Rebuilds the hash based on the current hash values for each key. If
values of key objects have changed since they were inserted, this
method will reindex <i>hsh</i>.

   h = {"AAA" => "b"}
   h.keys[0].chop!
   h          #=> {"AA"=>"b"}
   h["AA"]    #=> nil
   h.rehash   #=> {"AA"=>"b"}
   h["AA"]    #=> "b"


337
338
339
340
341
342
343
# File 'mrblib/hash.rb', line 337

def rehash
  h = {}
  self.each{|k,v|
    h[k] = v
  }
  self.replace(h)
end

- (Object) reject(&b)

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.


257
258
259
260
261
262
263
264
265
266
267
# File 'mrblib/hash.rb', line 257

def reject(&b)
  return to_enum :reject unless block_given?

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

- (Object) reject!(&b)

call-seq:

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

Equivalent to <code>Hash#delete_if</code>, but returns
<code>nil</code> if no changes were made.

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


226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'mrblib/hash.rb', line 226

def reject!(&b)
  return to_enum :reject! unless block_given?

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

- (Object) replace(hash) Also known as: initialize_copy

Replaces the contents of hsh with the contents of other hash

ISO 15.2.13.4.23



156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'mrblib/hash.rb', line 156

def replace(hash)
  self.clear
  hash = hash.to_hash
  hash.each_key{|k|
    self[k] = hash[k]
  }
  if hash.default_proc
    self.default_proc = hash.default_proc
  else
    self.default = hash.default
  end
  self
end

- (Object) select(&b)

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


310
311
312
313
314
315
316
317
318
319
320
# File 'mrblib/hash.rb', line 310

def select(&b)
  return to_enum :select unless block_given?

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

- (Object) select!(&b)

call-seq:

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

Equivalent to <code>Hash#keep_if</code>, but returns
<code>nil</code> if no changes were made.

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


279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'mrblib/hash.rb', line 279

def select!(&b)
  return to_enum :select! unless block_given?

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