Ruby each.with_index(1) returns error: no block given (yield)

651 views Asked by At

Trying to iterate table rows, skipping the first row. Using Ruby, Cucumber and Page-object gem.

PAGE_CLASS

  table(:table_data, id: 'list')

STEP DEFINITION

@current_page.table_data_element.each.with_index(1) do |row|
  puts row.value
end

getting error message: LocalJumpError: no block given (yield)

3

There are 3 answers

0
codesman On BEST ANSWER

This code worked for me.

   @current_page.table_data_element.each do |row|
      if row.text == ''
        next
      else
        row.link_element(text: 'Edit').visible?
      end
    end
1
akuhn On

Try this

array.drop(1).each do |row|
  ...
end

How does this work?

  • drop skips n elements, this does not modify the original array
  • each enumerates over all remaining elements
7
OneNeptune On

A clean way to do this without modifying the array would be:

array[1..-1].each { |row| }

array[1..-1] specifies a range of indexes within the array, starting at index 1 (since 0 would be the index of the first element within the array) and going through to the last index (-1).

Edit: OP has updated that the information returned isn't an array, the following syntax should work as the question intends:

@current_page.table_data_element.each.with_index(1) do |row, index|
  puts row.value
end