In Files

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

Hash

Public Class Methods

new(*args, &block) click to toggle source

Create a direct instance of the class Hash.

ISO 15.2.13.4.16

 
               # File mrblib/hash.rb, line 102
def initialize(*args, &block)
  self.__init_core(block, *args)
end
            

Public Instance Methods

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 15
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 45
def each(&block)
  self.keys.each{|k| block.call([k, self[k]])}
  self
end
            
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 69
def each_key(&block)
  self.keys.each{|k| block.call(k)}
  self
end
            
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 93
def each_value(&block)
  self.keys.each{|k| block.call(self[k])}
  self
end
            
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 114
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
            
reject(&b) click to toggle source

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

 
               # File mrblib/hash.rb, line 146
def reject(&b)
  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 130
def reject!(&b)
  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
            
select(&b) click to toggle source

1.9 #select returns Hash; ISO says nothing.

 
               # File mrblib/hash.rb, line 174
def select(&b)
  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 158
def select!(&b)
  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
            

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

If you are posting code samples in your comments, please wrap them in "<pre><code class="ruby" > ... </code></pre>" markup in order to get syntax highlighting.

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 a bug report so that it can be corrected for the next release. Thank you.

blog comments powered by Disqus