ruby-changes:38764
From: nobu <ko1@a...>
Date: Fri, 12 Jun 2015 17:34:36 +0900 (JST)
Subject: [ruby-changes:38764] nobu:r50845 (trunk): hash.c: fetch_values
nobu 2015-06-12 17:34:17 +0900 (Fri, 12 Jun 2015) New Revision: 50845 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=50845 Log: hash.c: fetch_values * hash.c (rb_hash_fetch_values): add `Hash#fetch_values`. [Feature #10017] [Fix GH-776] Modified files: trunk/ChangeLog trunk/hash.c trunk/test/ruby/test_hash.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 50844) +++ ChangeLog (revision 50845) @@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Fri Jun 12 17:34:14 2015 Wojciech Mach <wojtek@w...> + + * hash.c (rb_hash_fetch_values): add `Hash#fetch_values`. + [Feature #10017] [Fix GH-776] + Fri Jun 12 16:28:17 2015 Radan Skoric <radan.skoric@g...> * array.c (rb_ary_bsearch_index): Implement Array#bsearch_index Index: hash.c =================================================================== --- hash.c (revision 50844) +++ hash.c (revision 50845) @@ -1275,6 +1275,34 @@ rb_hash_values_at(int argc, VALUE *argv, https://github.com/ruby/ruby/blob/trunk/hash.c#L1275 return result; } +/* + * call-seq: + * hsh.fetch_values(key, ...) -> array + * hsh.fetch_values(key, ...) { |key| block } -> array + * + * Returns an array containing the values associated with the given keys + * but also raises <code>KeyError</code> when one of keys can't be found. + * Also see <code>Hash#values_at</code> and <code>Hash#fetch</code>. + * + * h = { "cat" => "feline", "dog" => "canine", "cow" => "bovine" } + * + * h.fetch_values("cow", "cat") #=> ["bovine", "feline"] + * h.fetch_values("cow", "bird") # raises KeyError + * h.fetch_values("cow", "bird") { |k| k.upcase } #=> ["bovine", "BIRD"] + */ + +VALUE +rb_hash_fetch_values(int argc, VALUE *argv, VALUE hash) +{ + VALUE result = rb_ary_new2(argc); + long i; + + for (i=0; i<argc; i++) { + rb_ary_push(result, rb_hash_fetch(hash, argv[i])); + } + return result; +} + static int select_i(VALUE key, VALUE value, VALUE result) { @@ -4001,6 +4029,7 @@ Init_Hash(void) https://github.com/ruby/ruby/blob/trunk/hash.c#L4029 rb_define_method(rb_cHash,"keys", rb_hash_keys, 0); rb_define_method(rb_cHash,"values", rb_hash_values, 0); rb_define_method(rb_cHash,"values_at", rb_hash_values_at, -1); + rb_define_method(rb_cHash,"fetch_values", rb_hash_fetch_values, -1); rb_define_method(rb_cHash,"shift", rb_hash_shift, 0); rb_define_method(rb_cHash,"delete", rb_hash_delete_m, 1); Index: test/ruby/test_hash.rb =================================================================== --- test/ruby/test_hash.rb (revision 50844) +++ test/ruby/test_hash.rb (revision 50845) @@ -498,6 +498,21 @@ class TestHash < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_hash.rb#L498 assert_equal ['three', nil, 'one', 'nil'], res end + def test_fetch_values + res = @h.fetch_values + assert_equal(0, res.length) + + res = @h.fetch_values(3, 2, 1, nil) + assert_equal(4, res.length) + assert_equal %w( three two one nil ), res + + assert_raises KeyError do + @h.fetch_values(3, 'invalid') + end + + res = @h.fetch_values(3, 'invalid') { |k| k.upcase } + assert_equal %w( three INVALID ), res + end def test_invert h = @h.invert -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/