Sha256: f585346534be41cca5d81fd7621868d1878a62390db5ac4c4426b32b360797a7

Contents?: true

Size: 807 Bytes

Versions: 29

Compression:

Stored size: 807 Bytes

Contents

describe "Hash#fetch" do
  it "returns the value for key" do
    {:a => 1, :b => -1}.fetch(:b).should == -1
  end

  it "raises and KeyError if key is not found" do
    lambda { Hash.new.fetch(:a) }.should raise_error(KeyError)
    lambda { Hash.new(5).fetch(:a) }.should raise_error(KeyError)
  end

  it "returns default if key is not found when passed a default" do
    Hash.new.fetch(:a, nil).should == nil
    Hash.new.fetch(:a, 'not here!').should == "not here!"
    {:a => nil}.fetch(:a, 'not here!').should == nil
  end

  it "returns value of block if key is not found when passed a block" do
    {}.fetch('a') { |k| k + '!' }.should == "a!"
  end

  it "gives precedence to the default block over the default argument when passed both" do
    {}.fetch(9, :foo) { |i| i * i }.should == 81
  end
end

Version data entries

29 entries across 29 versions & 1 rubygems

Version Path
opal-0.4.4 spec/rubyspec/core/hash/fetch_spec.rb
opal-0.4.3 spec/rubyspec/core/hash/fetch_spec.rb
opal-0.4.2 spec/rubyspec/core/hash/fetch_spec.rb
opal-0.4.1 spec/rubyspec/core/hash/fetch_spec.rb
opal-0.4.0 spec/rubyspec/core/hash/fetch_spec.rb
opal-0.3.44 spec/rubyspec/core/hash/fetch_spec.rb
opal-0.3.43 spec/rubyspec/core/hash/fetch_spec.rb
opal-0.3.42 spec/core/hash/fetch_spec.rb
opal-0.3.41 spec/core/hash/fetch_spec.rb
opal-0.3.40 spec/core/hash/fetch_spec.rb
opal-0.3.39 spec/core/hash/fetch_spec.rb
opal-0.3.38 spec/core/hash/fetch_spec.rb
opal-0.3.37 spec/core/hash/fetch_spec.rb
opal-0.3.36 spec/core/hash/fetch_spec.rb
opal-0.3.35 spec/core/hash/fetch_spec.rb
opal-0.3.34 spec/core/hash/fetch_spec.rb
opal-0.3.33 spec/core/hash/fetch_spec.rb
opal-0.3.32 spec/core/hash/fetch_spec.rb
opal-0.3.31 spec/core/hash/fetch_spec.rb
opal-0.3.30 spec/core/hash/fetch_spec.rb