How to get multiple columns by using `Roo` gem in one-line

1.5k views Asked by At

I want to get second to fourth column of a Excel file by using Roo::Excel class.

columns = []
columns << Roo::Excel.new("foo.xls").column(2)
columns << Roo::Excel.new("foo.xls").column(3)
columns << Roo::Excel.new("foo.xls").column(4)

I'm writing code redundantly because Roo::Excel.new("foo.xls") itself doesn't return a value even if I wait per minutes, but if I chain the method column() then it returns a Array instantly.

Is there a DRY way to write the code above?

This is the Excel file I'm trying to read.

http://www.tse.or.jp/listing/kessan/b7gje600000057pv-att/kessan10_1121.xls

1

There are 1 answers

0
Hetal Khunti On BEST ANSWER

How about this Ruby's range ?

columns = []
(2..4).each do |r|
 columns << Roo::Excel.new("foo.xls").column(r)
end