From 06eff9ca56be701882e2bcec7dc97b6b4929d68b Mon Sep 17 00:00:00 2001 From: kakubin Date: Tue, 12 Sep 2023 19:09:44 +0900 Subject: [PATCH] Fix Roo::Base#each_with_pagename degraded at #576 --- CHANGELOG.md | 1 + lib/roo/base.rb | 8 ++++---- spec/lib/roo/base_spec.rb | 20 ++++++++++++++++---- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c04c944d..ada61626 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ### Changed/Added - Prevent warnings on Ruby 3.1 if finalizer is called twice [586](https://github.com/roo-rb/roo/pull/586) +- Fix Roo::Base#each_with_pagename degraded at [576](https://github.com/roo-rb/roo/pull/576) [583](https://github.com/roo-rb/roo/pull/583) ## [2.10.0] 2023-02-07 diff --git a/lib/roo/base.rb b/lib/roo/base.rb index 91c8d1cf..80b495e0 100644 --- a/lib/roo/base.rb +++ b/lib/roo/base.rb @@ -250,10 +250,10 @@ 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 + return to_enum(:each_with_pagename) { sheets.size } unless block_given? + + sheets.each do |s| + yield sheet(s, true) end end diff --git a/spec/lib/roo/base_spec.rb b/spec/lib/roo/base_spec.rb index 9d44656b..41229650 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 << 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