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
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
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
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
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
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
# 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
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.