From bc85c8d8529b58c5c649f418ca549569ba348caa Mon Sep 17 00:00:00 2001 From: Jean Boussier Date: Thu, 5 Sep 2024 20:00:32 +0200 Subject: [PATCH] Implement Array#fetch_values [Feature #20702] Works the same way than `Hash#fetch_values` for for array. --- array.rb | 22 +++++++++++ spec/ruby/core/array/fetch_values_spec.rb | 48 +++++++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 spec/ruby/core/array/fetch_values_spec.rb diff --git a/array.rb b/array.rb index 9bfd7e9e9a..9a47c0eab2 100644 --- a/array.rb +++ b/array.rb @@ -210,4 +210,26 @@ class Array end end end + + # call-seq: + # array.fetch_values(*indexes) -> new_array + # array.fetch_values(*indexes) {|key| ... } -> new_array + # + # Returns a new Array containing the values associated with the given indexes *indexes: + # a = [:foo, :bar, :baz] + # a.fetch_values(3, 1) # => [:baz, :foo] + # + # Returns a new empty Array if no arguments given. + # + # When a block is given, calls the block with each missing index, + # treating the block's return value as the value for that index: + # a = [:foo, :bar, :baz] + # values = a.fetch_values(1, 0, 42, 777) {|index| index.to_s} + # values # => [:bar, :foo, "42", "777"] + # + # When no block is given, raises an exception if any given key is not found. + def fetch_values(*indexes, &block) + indexes.map! { |i| fetch(i, &block) } + indexes + end end diff --git a/spec/ruby/core/array/fetch_values_spec.rb b/spec/ruby/core/array/fetch_values_spec.rb new file mode 100644 index 0000000000..559b6c2b2f --- /dev/null +++ b/spec/ruby/core/array/fetch_values_spec.rb @@ -0,0 +1,48 @@ +require_relative '../../spec_helper' +require_relative 'fixtures/classes' + +describe "Array#fetch_values" do + before :each do + @array = [:a, :b, :c] + end + + ruby_version_is "3.4" do + describe "with matched indexes" do + it "returns the values for indexes" do + @array.fetch_values(0).should == [:a] + @array.fetch_values(0, 2).should == [:a, :c] + end + + it "returns the values for indexes ordered in the order of the requested indexes" do + @array.fetch_values(2, 0).should == [:c, :a] + end + end + + describe "with unmatched indexes" do + it "raises a index error if no block is provided" do + -> { @array.fetch_values(0, 1, 44) }.should raise_error(IndexError) + end + + it "returns the default value from block" do + @array.fetch_values(44) { |index| "`#{index}' is not found" }.should == ["`44' is not found"] + @array.fetch_values(0, 44) { |index| "`#{index}' is not found" }.should == [:a, "`44' is not found"] + end + end + + describe "without keys" do + it "returns an empty Array" do + @array.fetch_values.should == [] + end + end + + it "tries to convert the passed argument to an Integer using #to_int" do + obj = mock('to_int') + obj.should_receive(:to_int).and_return(2) + @array.fetch_values(obj).should == [:c] + end + + it "raises a TypeError when the passed argument can't be coerced to Integer" do + -> { [].fetch_values("cat") }.should raise_error(TypeError) + end + end +end