From 099d26f33c894ca48123f8e4de207b50cda975f2 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Thu, 3 Aug 2017 15:54:24 -0400 Subject: [PATCH] Add Rscons::VarSet#values_at - close #45 --- lib/rscons/varset.rb | 13 +++++++++++++ spec/rscons/varset_spec.rb | 9 +++++++++ 2 files changed, 22 insertions(+) diff --git a/lib/rscons/varset.rb b/lib/rscons/varset.rb index 38647cb..2787fec 100644 --- a/lib/rscons/varset.rb +++ b/lib/rscons/varset.rb @@ -143,6 +143,19 @@ module Rscons end end + # Return an array containing the values associated with the given keys. + # + # @param keys [Array] + # Keys to look up in the VarSet. + # + # @return [Array] + # An array containing the values associated with the given keys. + def values_at(*keys) + keys.map do |key| + self[key] + end + end + private # Move all VarSet variables into the copy-on-access list. diff --git a/spec/rscons/varset_spec.rb b/spec/rscons/varset_spec.rb index 32dd574..4e0d479 100644 --- a/spec/rscons/varset_spec.rb +++ b/spec/rscons/varset_spec.rb @@ -199,5 +199,14 @@ module Rscons end end + describe "#values_at" do + it "returns an array of the values associated with the given keys" do + v = VarSet.new({"fuz" => "a string", "foo" => 42, "bar" => :baz, + "qax" => [3, 6], "qux" => {a: :b}}) + expect(v.values_at).to eq [] + expect(v.values_at(*%w[fuz qux qax])).to eq ["a string", {a: :b}, [3, 6]] + end + end + end end