Skip to content

Instantly share code, notes, and snippets.

@MatthewRDodds
Created December 18, 2019 15:24
Show Gist options
  • Save MatthewRDodds/a628168db9e7c37aeda4e01142c8c12b to your computer and use it in GitHub Desktop.
Save MatthewRDodds/a628168db9e7c37aeda4e01142c8c12b to your computer and use it in GitHub Desktop.
Markdown Table Generator Script
#
# Usage:
#
# rows = [['User ID', 'Email'], ['1', 'example@email.com'],[2,'someone@email.com']]
# MarkdownTableGenerator.generate(rows)
#
module MarkdownTableGenerator
extend self
PIPE = '|'.freeze
DASH = '-'.freeze
def pipify(row_values)
row_values.join(PIPE).prepend(PIPE).concat(PIPE)
end
def generate(rows)
header_row = rows.shift
puts <<~EOS
#{pipify(header_row)}
#{pipify((DASH * header_row.size).split(''))}
#{rows.map { |row| pipify(row) }.join("\n")}
EOS
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment