In Files

  • mrbgems/mruby-hash-ext/mrblib/hash.rb
  • mrblib/hash.rb

Hash

Hash is enumerable

ISO 15.2.13.3

Public Instance Methods

==(hash) click to toggle source
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

 
               # 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
            
__update(h) click to toggle source
 
               # File mrblib/hash.rb, line 265
def __update(h)
  h.each_key{|k| self[k] = h[k]}
  self
end
            
delete(key, &block) click to toggle source

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

 
               # 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
            
each {| key, value | block } → hsh click to toggle source
each_pair {| key, value | block } → hsh
each → an_enumerator
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

 
               # File mrblib/hash.rb, line 86
def each(&block)
  return to_enum :each unless block_given?

  self.keys.each { |k| block.call [k, self[k]] }
  self
end
            
Also aliased as: each_pair
each_key {| key | block } → hsh click to toggle source
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

 
               # File mrblib/hash.rb, line 112
def each_key(&block)
  return to_enum :each_key unless block_given?

  self.keys.each{|k| block.call(k)}
  self
end
            
each_pair(&block) click to toggle source
Alias for: each
each_value {| value | block } → hsh click to toggle source
each_value → an_enumerator

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

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

 
               # File mrblib/hash.rb, line 138
def each_value(&block)
  return to_enum :each_value unless block_given?

  self.keys.each{|k| block.call(self[k])}
  self
end
            
eql?(hash) click to toggle source

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

ISO 15.2.13.4.32 (x)

 
               # 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
            
initialize_copy(hash) click to toggle source

ISO 15.2.13.4.17

Alias for: replace
inspect() click to toggle source

Return the contents of this hash as a string.

ISO 15.2.13.4.30 (x)

 
               # File mrblib/hash.rb, line 192
def inspect
  return "{}" if self.size == 0
  "{"+self.map {|k,v|
    k.inspect + "=>" + v.inspect
  }.join(", ")+"}"
end
            
Also aliased as: to_s
merge(other, &block) click to toggle source

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

 
               # File mrblib/hash.rb, line 173
def merge(other, &block)
  h = {}
  raise "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
            
merge!(other, &block) click to toggle source
 
               # File mrbgems/mruby-hash-ext/mrblib/hash.rb, line 2
def merge!(other, &block)
  raise "can't convert argument into Hash" unless other.respond_to?(:to_hash)
  if block
    other.each_key{|k|
      self[k] = (self.has_key?(k))? block.call(k, self[k], other[k]): other[k]
    }
  else
    other.each_key{|k| self[k] = other[k]}
  end
  self
end
            
Also aliased as: update
reject(&b) click to toggle source

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

 
               # File mrblib/hash.rb, line 220
def reject(&b)
  return to_enum :reject unless block_given?

  h = {}
  self.each_key{|k|
    v = self[k]
    unless b.call([k, v])
      h[k] = v
    end
  }
  h
end
            
reject!(&b) click to toggle source

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

 
               # File mrblib/hash.rb, line 202
def reject!(&b)
  return to_enum :reject! unless block_given?

  keys = []
  self.each_key{|k|
    v = self[k]
    if b.call([k, v])
      keys.push(k)
    end
  }
  return nil if keys.size == 0
  keys.each{|k|
    self.delete(k)
  }
  self
end
            
replace(hash) click to toggle source

Replaces the contents of hsh with the contents of other hash

ISO 15.2.13.4.23

 
               # File mrblib/hash.rb, line 149
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
            
Also aliased as: initialize_copy
select(&b) click to toggle source

1.9 #select returns Hash; ISO says nothing.

 
               # File mrblib/hash.rb, line 252
def select(&b)
  return to_enum :select unless block_given?

  h = {}
  self.each_key{|k|
    v = self[k]
    if b.call([k, v])
      h[k] = v
    end
  }
  h
end
            
select!(&b) click to toggle source

1.9 #select! returns Hash; ISO says nothing.

 
               # File mrblib/hash.rb, line 234
def select!(&b)
  return to_enum :select! unless block_given?

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

ISO 15.2.13.4.31 (x)

Alias for: inspect
update(other, &block) click to toggle source
Alias for: merge!

Commenting is here to help enhance the documentation. For example, code samples, or clarification of the documentation.

If you have questions about Ruby or the documentation, please post to one of the Ruby mailing lists. You will get better, faster, help that way.

If you wish to post a correction of the docs, please do so, but also file bug report so that it can be corrected for the next release. Thank you.

If you want to help improve the Ruby documentation, please visit Documenting-ruby.org.

blog comments powered by Disqus