1

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.

2
  • Do try and allow a little more room for names and email addresses. Unless you have a very compelling reason to restrict something, use VARCHAR(255) as a default. Commented Sep 9, 2016 at 1:47
  • Yes, I know, I use VARCHAR(255), I just copied that as an example. Commented Sep 9, 2016 at 5:05

2 Answers 2

1

table_print can't print nested arrays like this:

arrays = [
  ['value1', 'value2', 'value3'],
  ['value4', 'value5', 'value6']
]

because it flattens the input. You have to convert the inner arrays to another object.

Hash would work:

hashes = array.map { |values| %w(col1 col2 col3).zip(values).to_h }
#=> [
#     {"col1"=>"value1", "col2"=>"value2", "col3"=>"value3"},
#     {"col1"=>"value4", "col2"=>"value5", "col3"=>"value6"}
#   ]

tp hashes
# COL1   | COL2   | COL3
# -------|--------|-------
# value1 | value2 | value3
# value4 | value5 | value6

Struct would work as well:

Row = Struct.new(:col1, :col2, :col3)
rows = arrays.map { |values| Row.new(*values) }
#=> [
#     #<struct Row col1="value1", col2="value2", col3="value3">,
#     #<struct Row col1="value4", col2="value5", col3="value6">
#   ]

tp rows
# COL1   | COL2   | COL3
# -------|--------|-------
# value1 | value2 | value3
# value4 | value5 | value6
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.