Project

General

Profile

Feature #420 ยป hash-set-default-proc-1.8.7.patch

Patch to implement Hash#default_proc= against svn branch ruby_1_8_7@18504 - oblomov (Giuseppe Bilotta), 08/12/2008 08:41 AM

View differences:

hash.c (working copy)
613 613
    return Qnil;
614 614
}
615 615

  
616
extern VALUE rb_obj_is_proc(VALUE);
617
/*
618
 *  call-seq:
619
 *     hsh.default_proc = proc_obj     => proc_obj
620
 *
621
 *  Sets the default proc to be executed on each key lookup.
622
 *
623
 *     h.default_proc = proc do |hash, key|
624
 *       hash[key] = key + key
625
 *     end
626
 *     h[2]       #=> 4
627
 *     h["cat"]   #=> "catcat"
628
 */
629

  
630
static VALUE
631
rb_hash_set_default_proc(hash, proc)
632
	VALUE hash, proc;
633
{
634
    rb_hash_modify(hash);
635
    if (!rb_obj_is_proc(proc)) {
636
	    rb_raise(rb_eTypeError, "default_proc must be Proc");
637
    }
638
    RHASH(hash)->ifnone = proc;
639
    FL_SET(hash, HASH_PROC_DEFAULT);
640
    return proc;
641
}
642

  
616 643
static int
617 644
index_i(key, value, args)
618 645
    VALUE key, value;
......
2666 2693
    rb_define_method(rb_cHash,"default", rb_hash_default, -1);
2667 2694
    rb_define_method(rb_cHash,"default=", rb_hash_set_default, 1);
2668 2695
    rb_define_method(rb_cHash,"default_proc", rb_hash_default_proc, 0);
2696
    rb_define_method(rb_cHash,"default_proc=", rb_hash_set_default_proc, 1);
2669 2697
    rb_define_method(rb_cHash,"index", rb_hash_index, 1);
2670 2698
    rb_define_method(rb_cHash,"indexes", rb_hash_indexes, -1);
2671 2699
    rb_define_method(rb_cHash,"indices", rb_hash_indexes, -1);
eval.c (working copy)
2553 2553

  
2554 2554
static void blk_free();
2555 2555

  
2556
static VALUE
2556
VALUE
2557 2557
rb_obj_is_proc(proc)
2558 2558
    VALUE proc;
2559 2559
{