Bug #419 ยป hash-set-default-proc-1.9.patch
| hash.c (working copy) | ||
|---|---|---|
|
**********************************************************************/
|
||
|
#include "eval_intern.h"
|
||
|
#include "ruby/ruby.h"
|
||
|
#include "ruby/st.h"
|
||
|
#include "ruby/util.h"
|
||
| ... | ... | |
|
return Qnil;
|
||
|
}
|
||
|
/*
|
||
|
* call-seq:
|
||
|
* hsh.default_proc = proc_obj => proc_obj
|
||
|
*
|
||
|
* Sets the default proc to be executed on each key lookup.
|
||
|
*
|
||
|
* h.default_proc = proc do |hash, key|
|
||
|
* hash[key] = key + key
|
||
|
* end
|
||
|
* h[2] #=> 4
|
||
|
* h["cat"] #=> "catcat"
|
||
|
*/
|
||
|
static VALUE
|
||
|
rb_hash_set_default_proc(VALUE hash, VALUE proc)
|
||
|
{
|
||
|
rb_hash_modify(hash);
|
||
|
if (!rb_obj_is_proc(proc)) {
|
||
|
rb_raise(rb_eTypeError, "default_proc must be Proc");
|
||
|
}
|
||
|
RHASH(hash)->ifnone = proc;
|
||
|
FL_SET(hash, HASH_PROC_DEFAULT);
|
||
|
return proc;
|
||
|
}
|
||
|
static int
|
||
|
key_i(VALUE key, VALUE value, VALUE *args)
|
||
|
{
|
||
| ... | ... | |
|
rb_define_method(rb_cHash,"default", rb_hash_default, -1);
|
||
|
rb_define_method(rb_cHash,"default=", rb_hash_set_default, 1);
|
||
|
rb_define_method(rb_cHash,"default_proc", rb_hash_default_proc, 0);
|
||
|
rb_define_method(rb_cHash,"default_proc=", rb_hash_set_default_proc, 1);
|
||
|
rb_define_method(rb_cHash,"key", rb_hash_key, 1);
|
||
|
rb_define_method(rb_cHash,"index", rb_hash_index, 1);
|
||
|
rb_define_method(rb_cHash,"size", rb_hash_size, 0);
|
||