Sha256: 82beafd56778a6cad1031d34d2ab44ed32cac88af8d9ef3b450414b16deacbd8

Contents?: true

Size: 1008 Bytes

Versions: 26

Compression:

Stored size: 1008 Bytes

Contents

describe "Hash#[]" do
  it "returns the value for the key" do
    obj = Object.new
    h = {1 => 2, 3 => 4, "foo" => "bar", obj => obj, [] => "baz"}
    h[1].should == 2
    h[3].should == 4
    h["foo"].should == "bar"
    h[obj].should == obj
  end

  it "returns nil as default default value" do
    {0 => 0}[5].should == nil
  end

  it "returns the default (immediate) value for missing keys" do
    h = Hash.new 5
    h[:a].should == 5
    h[:a] = 0
    h[:a].should == 0
    h[:b].should == 5
  end

  it "does not return default values for keys with nil values" do
    h = Hash.new 5
    h[:a] = nil
    h[:a].should == nil
  end

  it "returns the default (dynamic) value for missing keys" do
    h = Hash.new { |hsh, k| k.kind_of?(Numeric) ? hsh[k] = k + 2 : hsh[k] = k }
    h[1].should == 3
    h['this'].should == 'this'
    h.should == {1 => 3, 'this' => 'this'}

    i = 0
    h = Hash.new { |hsh, key| i += 1 }
    h[:foo].should == 1
    h[:foo].should == 2
    h[:bar].should == 3
  end
end

Version data entries

26 entries across 26 versions & 1 rubygems

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