summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-11-10 07:57:17 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-11-10 07:57:17 +0000
commitfbe967ec02cb65a7efa3fb8f3d747cf6f620dde1 (patch)
treec01bf08a7dfae15b3519ece7383fe0d74f005fff
parent911b1c6b06a89603dfc9976a021b85e505221b7b (diff)
hash.c: to_proc
* hash.c (rb_hash_to_proc): new method Hash#to_proc. [Feature #11653] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@52520 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog5
-rw-r--r--NEWS1
-rw-r--r--hash.c13
-rw-r--r--test/ruby/test_hash.rb10
4 files changed, 29 insertions, 0 deletions
diff --git a/ChangeLog b/ChangeLog
index 324d594..8d48613 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+Tue Nov 10 16:57:14 2015 Nobuyoshi Nakada <nobu@ruby-lang.org>
+
+ * hash.c (rb_hash_to_proc): new method Hash#to_proc.
+ [Feature #11653]
+
Tue Nov 10 14:34:09 2015 NARUSE, Yui <naruse@ruby-lang.org>
* time.c (rb_time_timespec_new): swap utc and localtime
diff --git a/NEWS b/NEWS
index c48f0f4..075c214 100644
--- a/NEWS
+++ b/NEWS
@@ -60,6 +60,7 @@ with all sufficient information, see the ChangeLog file.
* Hash#fetch_values [Feature #10017]
* Hash#dig [Feature #11643]
* Hash#<=, Hash#<, Hash#>=, Hash#> [Feature #10984]
+ * Hash#to_proc [Feature #11653]
* IO
diff --git a/hash.c b/hash.c
index beaaad6..5b1abbd 100644
--- a/hash.c
+++ b/hash.c
@@ -2766,6 +2766,18 @@ rb_hash_gt(VALUE hash, VALUE other)
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)
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);
diff --git a/test/ruby/test_hash.rb b/test/ruby/test_hash.rb
index 4f48d3b..cb59524 100644
--- a/test/ruby/test_hash.rb
+++ b/test/ruby/test_hash.rb
@@ -1333,6 +1333,16 @@ class TestHash < Test::Unit::TestCase
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(*)