[前][次][番号順一覧][スレッド一覧]

ruby-changes:41985

From: nobu <ko1@a...>
Date: Wed, 9 Mar 2016 16:24:51 +0900 (JST)
Subject: [ruby-changes:41985] nobu:r54058 (trunk): hash.c: SET_PROC_DEFAULT

nobu	2016-03-09 16:17:03 +0900 (Wed, 09 Mar 2016)

  New Revision: 54058

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=54058

  Log:
    hash.c: SET_PROC_DEFAULT
    
    * hash.c (SET_PROC_DEFAULT): new macro to set the default proc.

  Modified files:
    trunk/hash.c
Index: hash.c
===================================================================
--- hash.c	(revision 54057)
+++ hash.c	(revision 54058)
@@ -36,6 +36,8 @@ https://github.com/ruby/ruby/blob/trunk/hash.c#L36
     FL_UNSET_RAW(hash, HASH_PROC_DEFAULT), \
     RHASH_SET_IFNONE(hash, ifnone))
 
+#define SET_PROC_DEFAULT(hash, proc) set_proc_default(hash, proc)
+
 static VALUE
 has_extra_methods(VALUE klass)
 {
@@ -528,14 +530,19 @@ tbl_update(VALUE hash, VALUE key, tbl_up https://github.com/ruby/ruby/blob/trunk/hash.c#L530
     RHASH_UPDATE_ITER(hash, RHASH_ITER_LEV(hash), key, func, arg)
 
 static void
-default_proc_arity_check(VALUE proc)
+set_proc_default(VALUE hash, VALUE proc)
 {
-    int n = rb_proc_arity(proc);
+    if (rb_proc_lambda_p(proc)) {
+	int n = rb_proc_arity(proc);
 
-    if (rb_proc_lambda_p(proc) && n != 2 && (n >= 0 || n < -3)) {
-	if (n < 0) n = -n-1;
-	rb_raise(rb_eTypeError, "default_proc takes two arguments (2 for %d)", n);
+	if (n != 2 && (n >= 0 || n < -3)) {
+	    if (n < 0) n = -n-1;
+	    rb_raise(rb_eTypeError, "default_proc takes two arguments (2 for %d)", n);
+	}
     }
+
+    FL_SET_RAW(hash, HASH_PROC_DEFAULT);
+    RHASH_SET_IFNONE(hash, proc);
 }
 
 /*
@@ -582,9 +589,7 @@ rb_hash_initialize(int argc, VALUE *argv https://github.com/ruby/ruby/blob/trunk/hash.c#L589
     if (rb_block_given_p()) {
 	rb_check_arity(argc, 0, 0);
 	ifnone = rb_block_proc();
-	default_proc_arity_check(ifnone);
-	RHASH_SET_IFNONE(hash, ifnone);
-	FL_SET(hash, HASH_PROC_DEFAULT);
+	SET_PROC_DEFAULT(hash, ifnone);
     }
     else {
 	rb_check_arity(argc, 0, 1);
@@ -1006,9 +1011,7 @@ rb_hash_set_default_proc(VALUE hash, VAL https://github.com/ruby/ruby/blob/trunk/hash.c#L1011
 		 rb_obj_classname(proc));
     }
     proc = b;
-    default_proc_arity_check(proc);
-    RHASH_SET_IFNONE(hash, proc);
-    FL_SET(hash, HASH_PROC_DEFAULT);
+    SET_PROC_DEFAULT(hash, proc);
     return proc;
 }
 

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]