File: GH-2943_each_with_object.rb

package info (click to toggle)
jruby 9.4.8.0%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 89,244 kB
  • sloc: ruby: 548,574; java: 276,189; yacc: 25,873; ansic: 6,178; xml: 6,111; sh: 1,855; sed: 94; makefile: 78; jsp: 48; tcl: 40; exp: 12
file content (31 lines) | stat: -rw-r--r-- 1,051 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
describe 'Enumerable#each_with_object' do

  it 'reports argument error when no arguments given' do
    begin
      [].each_with_object
      fail 'ArgumentError not raised!'
    rescue ArgumentError => e
      # TODO JRuby does seem to get a different error message than MRI
      #expect( e.message ).to eql 'wrong number of arguments (0 for 1)'
    end
  end

  it 'works with a 2 arg lambda passed as block' do
    storategy = lambda { |str, hash| hash[str] = str }
    result = %w(a b c d e f).each_with_object({}, &storategy)
    expected = {"a"=>"a", "b"=>"b", "c"=>"c", "d"=>"d", "e"=>"e", "f"=>"f"}
    expect( result ).to eql expected
  end

  it 'works just fine with a block' do
    result = %w(a b c d e f).each_with_object({}) { |v, hash| hash[v] = v }
    expected = {"a"=>"a", "b"=>"b", "c"=>"c", "d"=>"d", "e"=>"e", "f"=>"f"}
    expect( result ).to eql expected

    other = []
    result = %w(a b c d e f).each_with_object({}) { |v| other << v }
    expect( result ).to be_empty
    expect( other ).to eql %w(a b c d e f)
  end

end