1require_relative '../../spec_helper' 2 3describe "Hash#rassoc" do 4 before :each do 5 @h = {apple: :green, orange: :orange, grape: :green, banana: :yellow} 6 end 7 8 it "returns an Array if the argument is a value of the Hash" do 9 @h.rassoc(:green).should be_an_instance_of(Array) 10 end 11 12 it "returns a 2-element Array if the argument is a value of the Hash" do 13 @h.rassoc(:orange).size.should == 2 14 end 15 16 it "sets the first element of the Array to the key of the located value" do 17 @h.rassoc(:yellow).first.should == :banana 18 end 19 20 it "sets the last element of the Array to the located value" do 21 @h.rassoc(:yellow).last.should == :yellow 22 end 23 24 it "only returns the first matching key-value pair" do 25 @h.rassoc(:green).should == [:apple, :green] 26 end 27 28 it "uses #== to compare the argument to the values" do 29 @h[:key] = 1.0 30 1.should == 1.0 31 @h.rassoc(1).should eql [:key, 1.0] 32 end 33 34 it "returns nil if the argument is not a value of the Hash" do 35 @h.rassoc(:banana).should be_nil 36 end 37 38 it "returns nil if the argument is not a value of the Hash even when there is a default" do 39 Hash.new(42).merge!( foo: :bar ).rassoc(42).should be_nil 40 Hash.new{|h, k| h[k] = 42}.merge!( foo: :bar ).rassoc(42).should be_nil 41 end 42end 43