Feature #4234 ยป hash-default-proc.patch
| hash.c | ||
|---|---|---|
|     VALUE b; | ||
|     rb_hash_modify(hash); | ||
|     if (NIL_P(proc)) { | ||
| 	FL_UNSET(hash, HASH_PROC_DEFAULT); | ||
| 	RHASH_IFNONE(hash) = proc; | ||
| 	return proc; | ||
|     } | ||
|     b = rb_check_convert_type(proc, T_DATA, "Proc", "to_proc"); | ||
|     if (NIL_P(b) || !rb_obj_is_proc(b)) { | ||
| 	rb_raise(rb_eTypeError, | ||
| test/ruby/test_hash.rb | ||
|---|---|---|
|   def test_default_proc | ||
|     h = Hash.new {|hh, k| hh + k + "baz" } | ||
|     assert_equal("foobarbaz", h.default_proc.call("foo", "bar")) | ||
|     assert_nil(h.default_proc = nil) | ||
|     assert_nil(h.default_proc) | ||
|     h.default_proc = ->(h, k){ true } | ||
|     assert(h[:nope]) | ||
|     h = {} | ||
|     assert_nil(h.default_proc) | ||
|   end | ||