Sha256: f57d6e3c06aff36c986c3c2eeca54b3c7dff7495e77a492c67f242cfb1ae49f6

Contents?: true

Size: 1.24 KB

Versions: 31

Compression:

Stored size: 1.24 KB

Contents

describe "Array#collect" do
  it "returns a copy of array with each element replaced by the value returned by block" do
    a = ['a', 'b', 'c', 'd']
    b = a.collect { |i| i + '!' }
    b.should == ['a!', 'b!', 'c!', 'd!']
    b.object_id.should_not == a.object_id
  end

  it "does not change self" do
    a = ['a', 'b', 'c', 'd']
    b = a.collect { |i| i + '!' }
    a.should == ['a', 'b', 'c', 'd']
  end

  it "returns the evaluated value of block if it broke in the block" do
    a = ['a', 'b', 'c', 'd']
    b = a.collect {|i|
      if i == 'c'
        break 0
      else
        i + '!'
      end
    }
    b.should == 0
  end
end

describe "Array#collect!" do
  it "replaces each element with the value returned by block" do
    a = [7, 9, 3, 5]
    a.collect! { |i| i - 1 }.should equal(a)
    a.should == [6, 8, 2, 4]
  end

  it "returns self" do
    a = [1, 2, 3, 4, 5]
    b = a.collect! {|i| i+1 }
    a.object_id.should == b.object_id
  end

  it "returns the evaluated value of block but its contents is partially modified, if it broke in the block" do
    a = ['a', 'b', 'c', 'd']
    b = a.collect! {|i|
      if i == 'c'
        break 0
      else
        i + '!'
      end
    }
    b.should == 0
    a.should == ['a!', 'b!', 'c', 'd']
  end
end

Version data entries

31 entries across 31 versions & 1 rubygems

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