sharma_a sharma_a - 3 months ago 23
Ruby Question

Display array in tabular form

I have an array in this way -

arr = ["0.5", " 2016-08-25 11:02:00 +0530", " test 1",
" 0.75", " 2016-08-25 11:02:00 +0530", " test 2"]


and I want it to be displayed in a tabular form like this -

0.5 11:02 test 1
0.75 11:02 test 2

Answer
a = ["0.5", " 2016-08-25 11:02:00 +0530", " test 1", " 0.75", " 2016-08-25 11:02:00 +0530", " test 2"]

a.each_slice(3) do |x, y, z|
  puts "#{x.strip} #{y[/\d\d:\d\d/]} #{z.strip}"
end
Comments