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

ruby-changes:40439

From: nobu <ko1@a...>
Date: Tue, 10 Nov 2015 16:57:35 +0900 (JST)
Subject: [ruby-changes:40439] nobu:r52520 (trunk): hash.c: to_proc

nobu	2015-11-10 16:57:17 +0900 (Tue, 10 Nov 2015)

  New Revision: 52520

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

  Log:
    hash.c: to_proc
    
    * hash.c (rb_hash_to_proc): new method Hash#to_proc.
      [Feature #11653]

  Modified files:
    trunk/ChangeLog
    trunk/NEWS
    trunk/hash.c
    trunk/test/ruby/test_hash.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 52519)
+++ ChangeLog	(revision 52520)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Tue Nov 10 16:57:14 2015  Nobuyoshi Nakada  <nobu@r...>
+
+	* hash.c (rb_hash_to_proc): new method Hash#to_proc.
+	  [Feature #11653]
+
 Tue Nov 10 14:34:09 2015  NARUSE, Yui  <naruse@r...>
 
 	* time.c (rb_time_timespec_new): swap utc and localtime
Index: hash.c
===================================================================
--- hash.c	(revision 52519)
+++ hash.c	(revision 52520)
@@ -2766,6 +2766,18 @@ rb_hash_gt(VALUE hash, VALUE other) https://github.com/ruby/ruby/blob/trunk/hash.c#L2766
     return hash_le(other, hash);
 }
 
+static VALUE
+hash_proc_call(VALUE key, VALUE hash, int argc, const VALUE *argv, VALUE passed_proc)
+{
+    return rb_hash_aref(hash, key);
+}
+
+static VALUE
+rb_hash_to_proc(VALUE hash)
+{
+    return rb_proc_new(hash_proc_call, hash);
+}
+
 static int path_tainted = -1;
 
 static char **origenviron;
@@ -4132,6 +4144,7 @@ Init_Hash(void) https://github.com/ruby/ruby/blob/trunk/hash.c#L4144
     rb_define_method(rb_cHash,"to_a", rb_hash_to_a, 0);
     rb_define_method(rb_cHash,"inspect", rb_hash_inspect, 0);
     rb_define_alias(rb_cHash, "to_s", "inspect");
+    rb_define_method(rb_cHash,"to_proc", rb_hash_to_proc, 0);
 
     rb_define_method(rb_cHash,"==", rb_hash_equal, 1);
     rb_define_method(rb_cHash,"[]", rb_hash_aref, 1);
Index: NEWS
===================================================================
--- NEWS	(revision 52519)
+++ NEWS	(revision 52520)
@@ -60,6 +60,7 @@ with all sufficient information, see the https://github.com/ruby/ruby/blob/trunk/NEWS#L60
   * Hash#fetch_values [Feature #10017]
   * Hash#dig [Feature #11643]
   * Hash#<=, Hash#<, Hash#>=, Hash#> [Feature #10984]
+  * Hash#to_proc [Feature #11653]
 
 * IO
 
Index: test/ruby/test_hash.rb
===================================================================
--- test/ruby/test_hash.rb	(revision 52519)
+++ test/ruby/test_hash.rb	(revision 52520)
@@ -1333,6 +1333,16 @@ class TestHash < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_hash.rb#L1333
     assert_not_operator(h2, :>, h2)
   end
 
+  def test_to_proc
+    h = {
+      1 => 10,
+      2 => 20,
+      3 => 30,
+    }
+
+    assert_equal([10, 20, 30], [1, 2, 3].map(&h))
+  end
+
   class TestSubHash < TestHash
     class SubHash < Hash
       def reject(*)

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

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