0

I have some files which has data in the hash format which I am trying to print as tabular format,

{
  :outerkey1 => "value1", 
  :innerhash => { 
    :doubleinnerhash => { 
      :key1 => "", 
      :key2 => true, 
      :key3 => 0, 
      :key4 => "line1\nline2\n line3\nline4\n"
     },
     :innerkey => "OK",
     :innerkey3 => 0
   }, 
   :outerkey2 => "value2", 
   :outerkye3 => "value3", 
   :outerkey4 => "value4"
}

In the above formatted data, wanted to parse outerkey and the hash value of doubleinnerhash and then print in the tabluar format to present it.

I have got some idea on Python with the help of Python - Printing a dictionary as a horizontal table with headers but if want to implement then I need to convert this Ruby hash to Python Dict format where will get data inconsistency issue.

Am expecting below formatted output,

|----------------------------------------------------|
|outerkey1 | key1   |   key2    |   key3    |   key4 |
|----------------------------------------------------|
|value1    |        |   true    |   0       |   line1|
|          |        |           |           |   line2|
|          |        |           |           |   line3|
|          |        |           |           |   line4|
|----------------------------------------------------|
|value2    |error   |   false   |   2       |   line1|
|          |        |           |           |   line2|
|          |        |           |           |   line3|
|          |        |           |           |   line4|
|----------------------------------------------------|

So is there any straight forward mechanism to make this work in Ruby?

Keith Bennett
  • 4,722
  • 1
  • 25
  • 35
Karthi1234
  • 949
  • 1
  • 8
  • 28

2 Answers2

0

I don't know of anything that will perform the processing of the nested data automatically, but this library can help with the fixed character width formatting for output: https://github.com/piotrmurach/tty-table.

That said, I'd recommend using JSON or YAML as much as possible -- is there a reason the files containing the data aren't using one of those formats? Or are they?

Keith Bennett
  • 4,722
  • 1
  • 25
  • 35
0

There are several gems usable but in most vases you will need to modify your input.

The gem text-table seems to a good candidate to me.

Here an example

require 'text-table'

table = Text::Table.new
table.head = ['A', 'B']
table.rows = [['a1', 'b1']]
table.rows << ['a2', 'b2']

table.to_s

#    +----+----+
#    | A  | B  |
#    +----+----+
#    | a1 | b1 |
#    | a2 | b2 |
#    +----+----+

So you have to transform your hash to a an array of arrays for this gem.

I won't work it out completely (where would be the fun for you ?) but here a piece to get you started..

h = {
  :outerkey1 => "value1", 
  :innerhash => { 
    :doubleinnerhash => { 
      :key1 => "", 
      :key2 => true, 
      :key3 => 0, 
      :key4 => "line1\nline2\n line3\nline4\n"
     },
     :innerkey => "OK",
     :innerkey3 => 0
   }, 
   :outerkey2 => "value2", 
   :outerkye3 => "value3", 
   :outerkey4 => "value4"
}

header = [h.keys.first, h[:innerhash][:doubleinnerhash].keys].flatten
rows   = h[:innerhash][:doubleinnerhash].values
peter
  • 41,770
  • 5
  • 64
  • 108