Objects_to_text.rb
This content was produced by an LLM and could include errors.
This script processes array data, dynamically creating a clean, tab-separated table output that handles differing lengths for all columns within the structured data.
data = [
{:id => "a", :val1 => [1, 2, 3], :val2 => [3, 4]},
{:id => "b", :val1 => [0], :val2 => [9, 8, 7]}
]
#=> [{id: "a", val1: [1, 2, 3], val2: [3, 4]},
#...
puts data.first.keys.join("\t")
id val1 val2
#=> nil
data.each do |entry|
id = entry[:id]
val1 = entry[:val1]
val2 = entry[:val2]
max_length = [val1.length, val2.length].max
max_length.times do |i|
id_output = (i == 0 ? id : "")
val1_output = (val1[i].nil? ? "" : val1[i])
val2_output = (val2[i].nil? ? "" : val2[i])
puts [id_output, val1_output, val2_output].join("\t")
end
end
a 1 3
2 4
3
b 0 9
8
7
#=> [{id: "a", val1: [1, 2, 3], val2: [3, 4]},
{id: "b", val1: [0], val2: [9, 8, 7]}]
Ruby 4.0.3