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

ruby-changes:7063

From: knu <ko1@a...>
Date: Wed, 13 Aug 2008 18:25:07 +0900 (JST)
Subject: [ruby-changes:7063] Ruby:r18580 (ruby_1_8): * hash.c (rb_hash_set_default_proc): Add Hash#default_proc=;

knu	2008-08-13 18:24:48 +0900 (Wed, 13 Aug 2008)

  New Revision: 18580

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=18580

  Log:
    * hash.c (rb_hash_set_default_proc): Add Hash#default_proc=;
      submitted by Giuseppe Bilotta.  #420
    
    * eval.c (rb_obj_is_proc), intern.h: Export rb_obj_is_proc().

  Modified files:
    branches/ruby_1_8/ChangeLog
    branches/ruby_1_8/NEWS
    branches/ruby_1_8/eval.c
    branches/ruby_1_8/hash.c
    branches/ruby_1_8/intern.h

Index: ruby_1_8/intern.h
===================================================================
--- ruby_1_8/intern.h	(revision 18579)
+++ ruby_1_8/intern.h	(revision 18580)
@@ -174,6 +174,7 @@
 void rb_dvar_push _((ID, VALUE));
 VALUE *rb_svar _((int));
 VALUE rb_eval_cmd _((VALUE, VALUE, int));
+VALUE rb_obj_is_proc _((VALUE));
 int rb_obj_respond_to _((VALUE, ID, int));
 int rb_respond_to _((VALUE, ID));
 void rb_interrupt _((void));
Index: ruby_1_8/hash.c
===================================================================
--- ruby_1_8/hash.c	(revision 18579)
+++ ruby_1_8/hash.c	(revision 18580)
@@ -613,6 +613,38 @@
     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(hash, proc)
+    VALUE hash, 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
 index_i(key, value, args)
     VALUE key, value;
@@ -2666,6 +2698,7 @@
     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,"index", rb_hash_index, 1);
     rb_define_method(rb_cHash,"indexes", rb_hash_indexes, -1);
     rb_define_method(rb_cHash,"indices", rb_hash_indexes, -1);
Index: ruby_1_8/NEWS
===================================================================
--- ruby_1_8/NEWS	(revision 18579)
+++ ruby_1_8/NEWS	(revision 18580)
@@ -26,6 +26,10 @@
 
     New method with which replaces #choice.
 
+  * Hash#default_proc=
+
+    New method.
+
 * set
 
   Set#classify
Index: ruby_1_8/ChangeLog
===================================================================
--- ruby_1_8/ChangeLog	(revision 18579)
+++ ruby_1_8/ChangeLog	(revision 18580)
@@ -1,3 +1,10 @@
+Wed Aug 13 18:18:16 2008  Akinori MUSHA  <knu@i...>
+
+	* hash.c (rb_hash_set_default_proc): Add Hash#default_proc=;
+	  submitted by Giuseppe Bilotta.  #420
+
+	* eval.c (rb_obj_is_proc), intern.h: Export rb_obj_is_proc().
+
 Tue Aug 12 18:50:58 2008  Akinori MUSHA  <knu@i...>
 
 	* ext/syslog/syslog.c (mSyslog_open): Use of Check_SafeStr() is
Index: ruby_1_8/eval.c
===================================================================
--- ruby_1_8/eval.c	(revision 18579)
+++ ruby_1_8/eval.c	(revision 18580)
@@ -2555,7 +2555,7 @@
 
 static void blk_free();
 
-static VALUE
+VALUE
 rb_obj_is_proc(proc)
     VALUE proc;
 {

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

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