Project

General

Profile

Feature #4234 ยป hash-default-proc.patch

runpaint (Run Paint Run Run), 01/06/2011 01:11 AM

View differences:

hash.c
709 709
    VALUE b;
710 710

  
711 711
    rb_hash_modify(hash);
712

  
713
    if (NIL_P(proc)) {
714
	FL_UNSET(hash, HASH_PROC_DEFAULT);
715
	RHASH_IFNONE(hash) = proc;
716
	return proc;
717
    }
718

  
712 719
    b = rb_check_convert_type(proc, T_DATA, "Proc", "to_proc");
713 720
    if (NIL_P(b) || !rb_obj_is_proc(b)) {
714 721
	rb_raise(rb_eTypeError,
test/ruby/test_hash.rb
712 712
  def test_default_proc
713 713
    h = Hash.new {|hh, k| hh + k + "baz" }
714 714
    assert_equal("foobarbaz", h.default_proc.call("foo", "bar"))
715
    assert_nil(h.default_proc = nil)
716
    assert_nil(h.default_proc)
717
    h.default_proc = ->(h, k){ true }
718
    assert(h[:nope])
715 719
    h = {}
716 720
    assert_nil(h.default_proc)
717 721
  end