mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* hash.c (rb_hash_set_default_proc): add new method. a patch from
Giuseppe Bilotta. #419 git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@18577 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
3e7d8bf9f8
commit
8d4086d773
2 changed files with 38 additions and 0 deletions
|
@ -3,6 +3,11 @@ Wed Aug 13 17:35:58 2008 Tanaka Akira <akr@fsij.org>
|
||||||
* transcode.c (transcode_restartable0): several local variables
|
* transcode.c (transcode_restartable0): several local variables
|
||||||
removed.
|
removed.
|
||||||
|
|
||||||
|
Wed Aug 13 17:35:23 2008 Yukihiro Matsumoto <matz@ruby-lang.org>
|
||||||
|
|
||||||
|
* hash.c (rb_hash_set_default_proc): add new method. a patch from
|
||||||
|
Giuseppe Bilotta. #419
|
||||||
|
|
||||||
Wed Aug 13 17:31:12 2008 Yukihiro Matsumoto <matz@ruby-lang.org>
|
Wed Aug 13 17:31:12 2008 Yukihiro Matsumoto <matz@ruby-lang.org>
|
||||||
|
|
||||||
* vm_insnhelper.c (caller_setup_args): should ensure if the value
|
* vm_insnhelper.c (caller_setup_args): should ensure if the value
|
||||||
|
|
33
hash.c
33
hash.c
|
@ -11,6 +11,7 @@
|
||||||
|
|
||||||
**********************************************************************/
|
**********************************************************************/
|
||||||
|
|
||||||
|
#include "eval_intern.h"
|
||||||
#include "ruby/ruby.h"
|
#include "ruby/ruby.h"
|
||||||
#include "ruby/st.h"
|
#include "ruby/st.h"
|
||||||
#include "ruby/util.h"
|
#include "ruby/util.h"
|
||||||
|
@ -623,6 +624,37 @@ rb_hash_default_proc(VALUE hash)
|
||||||
return Qnil;
|
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)
|
||||||
|
{
|
||||||
|
VALUE b;
|
||||||
|
|
||||||
|
rb_hash_modify(hash);
|
||||||
|
b = rb_check_convert_type(proc, T_DATA, "Proc", "to_proc");
|
||||||
|
if (NIL_P(b) || !rb_obj_is_proc(b)) {
|
||||||
|
rb_raise(rb_eTypeError,
|
||||||
|
"wrong default_proc type %s (expected Proc)",
|
||||||
|
rb_obj_classname(proc));
|
||||||
|
}
|
||||||
|
proc = b;
|
||||||
|
RHASH(hash)->ifnone = proc;
|
||||||
|
FL_SET(hash, HASH_PROC_DEFAULT);
|
||||||
|
return proc;
|
||||||
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
key_i(VALUE key, VALUE value, VALUE *args)
|
key_i(VALUE key, VALUE value, VALUE *args)
|
||||||
{
|
{
|
||||||
|
@ -2596,6 +2628,7 @@ Init_Hash(void)
|
||||||
rb_define_method(rb_cHash,"default", rb_hash_default, -1);
|
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=", 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_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,"key", rb_hash_key, 1);
|
||||||
rb_define_method(rb_cHash,"index", rb_hash_index, 1);
|
rb_define_method(rb_cHash,"index", rb_hash_index, 1);
|
||||||
rb_define_method(rb_cHash,"size", rb_hash_size, 0);
|
rb_define_method(rb_cHash,"size", rb_hash_size, 0);
|
||||||
|
|
Loading…
Reference in a new issue