Skip to content

Commit

Permalink
Make Sheet#each_row return Enumerator when no block
Browse files Browse the repository at this point in the history
This allows for chaining Enumerators, e.g.

    sheet.each.with_index do |row, index|
        # ...
    end

and matches behaviour of `Array#each`/`Hash#each`

    enum = [1, 2, 3].each # => #<Enumerator: ...>
    enum.peek # => 1
  • Loading branch information
myabc authored and martijn committed May 7, 2024
1 parent 72549f2 commit e5d18c6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/xsv/sheet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ def hidden?

# Iterate over rows, returning either hashes or arrays based on the current mode.
def each_row(&block)
return to_enum(__method__) unless block

@io.rewind
SheetRowsHandler.new(@mode, @headers, empty_row, @workbook, @row_skip, @last_row, &block).parse(@io)
true
Expand Down
11 changes: 11 additions & 0 deletions test/sheet_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,17 @@ def test_enumerable
assert_equal 2, filtered_rows.length
end

def test_each_enumerator
@workbook = Xsv.open("test/files/excel2016.xlsx")
sheet = @workbook.sheets[0]

last_row_index = nil
sheet.each.with_index do |row, index|
last_row_index = index
end
assert_equal 3, last_row_index
end

def test_empty
@workbook = Xsv.open("test/files/empty.xlsx")
sheet = @workbook.sheets[0]
Expand Down

0 comments on commit e5d18c6

Please sign in to comment.