Module: ForEachRow

Defined in:
lib/for_each_row.rb

Instance Method Summary collapse

Instance Method Details

#for_each_row(table, &block) ⇒ Object

Example: for_each_row <<-TABLE do |number1, number2, sum|

                       |1,       2,       3  |
                       |5,       7,       12 |
              TABLE
(number2 + number2).should == sum

end



10
11
12
13
14
15
16
17
# File 'lib/for_each_row.rb', line 10

def for_each_row(table, &block)
  table.split("\n").each do |row|
    if row.strip =~ /^\|(.*)\|$/
      cell_values = $1.split(",").map { |cell| eval(cell, block) }
      block.call *cell_values
    end
  end
end