new(...)
public
Returns a new, empty hash. If this hash is subsequently accessed by a key
that doesn’t correspond to a hash entry, the value returned depends
on the style of new used to
create the hash. In the first form, the access returns nil. If
obj is specified, this single object will be used for all default values. If a block is specified, it will
be called with the hash object and the key, and should return the default value. It is the block’s
responsibility to store the value in the
hash if required.
h = Hash.new("Go Fish")
h["a"] = 100
h["b"] = 200
h["a"]
h["c"]
h["c"].upcase!
h["d"]
h.keys
h = Hash.new { |hash, key| hash[key] = "Go Fish: #{key}" }
h["c"]
h["c"].upcase!
h["d"]
h.keys
Show source
/*
* call-seq:
* Hash.new => hash
* Hash.new(obj) => aHash
* Hash.new {|hash, key| block } => aHash
*
* Returns a new, empty hash. If this hash is subsequently accessed by
* a key that doesn't correspond to a hash entry, the value returned
* depends on the style of <code>new</code> used to create the hash. In
* the first form, the access returns <code>nil</code>. If
* <i>obj</i> is specified, this single object will be used for
* all <em>default values</em>. If a block is specified, it will be
* called with the hash object and the key, and should return the
* default value. It is the block's responsibility to store the value
* in the hash if required.
*
* h = Hash.new("Go Fish")
* h["a"] = 100
* h["b"] = 200
* h["a"] #=> 100
* h["c"] #=> "Go Fish"
* # The following alters the single default object
* h["c"].upcase! #=> "GO FISH"
* h["d"] #=> "GO FISH"
* h.keys #=> ["a", "b"]
*
* # While this creates a new default object each time
* h = Hash.new { |hash, key| hash[key] = "Go Fish: #{key}" }
* h["c"] #=> "Go Fish: c"
* h["c"].upcase! #=> "GO FISH: C"
* h["d"] #=> "Go Fish: d"
* h.keys #=> ["c", "d"]
*
*/
static VALUE
rb_hash_initialize(argc, argv, hash)
int argc;
VALUE *argv;
VALUE hash;
{
VALUE ifnone;
rb_hash_modify(hash);
if (rb_block_given_p()) {
if (argc > 0) {
rb_raise(rb_eArgError, "wrong number of arguments");
}
RHASH(hash)->ifnone = rb_block_proc();
FL_SET(hash, HASH_PROC_DEFAULT);
}
else {
rb_scan_args(argc, argv, "01", &ifnone);
RHASH(hash)->ifnone = ifnone;
}
return hash;
}