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

ruby-changes:23218

From: marcandre <ko1@a...>
Date: Mon, 9 Apr 2012 13:08:03 +0900 (JST)
Subject: [ruby-changes:23218] marcandRe: r35268 (trunk): * hash.c (rb_hash_set_default_proc): Accept nil, patch by Run Paint

marcandre	2012-04-09 13:07:53 +0900 (Mon, 09 Apr 2012)

  New Revision: 35268

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

  Log:
    * hash.c (rb_hash_set_default_proc): Accept nil, patch by Run Paint
      [Feature #4234]
    
    * test/ruby/test_hash.rb: test for above.

  Modified files:
    trunk/ChangeLog
    trunk/NEWS
    trunk/hash.c
    trunk/test/ruby/test_hash.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 35267)
+++ ChangeLog	(revision 35268)
@@ -1,3 +1,10 @@
+Mon Apr  9 13:06:58 2012  Marc-Andre Lafortune  <ruby-core@m...>
+
+	* hash.c (rb_hash_set_default_proc): Accept nil, patch by Run Paint
+	  [Feature #4234]
+
+	* test/ruby/test_hash.rb: test for above.
+
 Mon Apr  9 08:01:15 2012  Tadayoshi Funaba  <tadf@d...>
 
 	* ext/date/date_strftime.c: gets the value with range() consistetly.
Index: hash.c
===================================================================
--- hash.c	(revision 35267)
+++ hash.c	(revision 35268)
@@ -689,9 +689,9 @@
 
 /*
  *  call-seq:
- *     hsh.default_proc = proc_obj     -> proc_obj
+ *     hsh.default_proc = proc_obj or nil
  *
- *  Sets the default proc to be executed on each key lookup.
+ *  Sets the default proc to be executed on each failed key lookup.
  *
  *     h.default_proc = proc do |hash, key|
  *       hash[key] = key + key
@@ -706,6 +706,11 @@
     VALUE b;
 
     rb_hash_modify_check(hash);
+    if (NIL_P(proc)) {
+	FL_UNSET(hash, HASH_PROC_DEFAULT);
+	RHASH_IFNONE(hash) = proc;
+	return proc;
+    }
     b = rb_check_convert_type(proc, T_DATA, "Proc", "to_proc");
     if (NIL_P(b) || !rb_obj_is_proc(b)) {
 	rb_raise(rb_eTypeError,
Index: NEWS
===================================================================
--- NEWS	(revision 35267)
+++ NEWS	(revision 35268)
@@ -21,6 +21,10 @@
     * added method:
       * added Enumerable#lazy method for lazy enumeration.
 
+  * Hash
+    * extended method:
+      * Hash#default_proc= can be passed nil to clear the default proc.
+
   * Kernel
     * added method:
       * added Kernel#Hash conversion method like Array() or Float().
Index: test/ruby/test_hash.rb
===================================================================
--- test/ruby/test_hash.rb	(revision 35267)
+++ test/ruby/test_hash.rb	(revision 35268)
@@ -718,6 +718,10 @@
   def test_default_proc
     h = Hash.new {|hh, k| hh + k + "baz" }
     assert_equal("foobarbaz", h.default_proc.call("foo", "bar"))
+    assert_nil(h.default_proc = nil)
+    assert_nil(h.default_proc)
+    h.default_proc = ->(h, k){ true }
+    assert(h[:nope])
     h = {}
     assert_nil(h.default_proc)
   end

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

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