From f6491c663705d79d50d1687d592c2fcd4e1babb9 Mon Sep 17 00:00:00 2001 From: chaky Date: Thu, 2 Mar 2023 17:27:38 +0200 Subject: [PATCH] #ruby3_each_with_pagename fixed and test added --- lib/roo/base.rb | 7 ++----- spec/lib/roo/base_spec.rb | 20 ++++++++++++++++---- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/lib/roo/base.rb b/lib/roo/base.rb index 91c8d1cf..bcf7ac5a 100644 --- a/lib/roo/base.rb +++ b/lib/roo/base.rb @@ -250,11 +250,8 @@ def sheet(index, name = false) # iterate through all worksheets of a document def each_with_pagename - Enumerator.new do |yielder| - sheets.each do |s| - yielder << sheet(s, true) - end - end + return to_enum(:each_with_pagename) { sheets.size } unless block_given? + sheets.each { |x| yield(sheet(x, true)) } end # by passing in headers as options, this method returns diff --git a/spec/lib/roo/base_spec.rb b/spec/lib/roo/base_spec.rb index 9d44656b..d1f12879 100644 --- a/spec/lib/roo/base_spec.rb +++ b/spec/lib/roo/base_spec.rb @@ -183,10 +183,22 @@ def sheets end describe '#each_with_pagename' do - it 'should return an enumerator with all the rows' do - each_with_pagename = spreadsheet.each_with_pagename - expect(each_with_pagename).to be_a(Enumerator) - expect(each_with_pagename.to_a.last).to eq([spreadsheet.default_sheet, spreadsheet]) + context 'when block given' do + it 'iterate with sheet and sheet_name' do + sheet_names = [] + spreadsheet.each_with_pagename do |sheet_name, sheet| + sheet_names.push(sheet_name) + end + expect(sheet_names).to eq ['my_sheet', 'blank sheet'] + end + end + + context 'when called without block' do + it 'should return an enumerator with all the rows' do + each_with_pagename = spreadsheet.each_with_pagename + expect(each_with_pagename).to be_a(Enumerator) + expect(each_with_pagename.to_a.last).to eq([spreadsheet.default_sheet, spreadsheet]) + end end end