Forwarding Forwarding - 3 months ago 10
Ruby Question

How can I write table_print output within my Ruby script?

So, I've got a script, say, program.rb, and in it I want to output the table_print version of an array of lists

['value1','value2','value3']
['value4','value4','value6']


so that it looks like this in the .txt file I output

col1 | col2 | col3
-------------------------------------------------------------------------
value1 | value2 | value3
.
.
.


I already have table_print installed, but this is all I have so far as a working model:

require 'table_print'

TABLEPRINT STUFF?

open('table_print_output.txt','a'){|g|
g.puts TABLEPRINT?
}


I guess I'm just not getting how to do the Ruby equivalent of MySQL's create table

CREATE TABLE MyGuests (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
reg_date TIMESTAMP
)


and insert into

INSERT INTO table_name (column1, column2, column3,...)
VALUES (value1, value2, value3,...)


And I don't want a temporary/latent database just sitting around for no reason. It's like I need a database as a variable or something; that is, I create it, I populate it, I print it, I destroy it.

Answer

Looks like you need String#ljust:

rows = [
  ['value1','value2','value3'],
  ['value4','value4','value6']
]

rows.each do |row|
  puts "#{row[0].ljust(30)}|#{row[1].ljust(30)}|#{row[2].ljust(30)}"
end