Sha256: 943b3099f8ab2ddb5ea79609bd21d71226ed105ef8134dd6c43dbe856c3171f8

Contents?: true

Size: 1.02 KB

Versions: 61

Compression:

Stored size: 1.02 KB

Contents

# encoding: utf-8
require 'open_classes/string/justify_table'

# Array
class Array
  # Array(Array, Array...) to html table format.
  #
  # === Example
  #
  #   [['header1', 'header2', 'header3'],['line1_1', 'line1_2', 'line1_3']].to_html_table
  #
  # result
  #   <table>
  #     <tr>
  #       <th>header1</th>
  #       <th>header2</th>
  #       <th>header3</th>
  #     </tr>
  #     <tr>
  #       <td>line1_1</td>
  #       <td>line1_2</td>
  #       <td>line1_3</td>
  #     </tr>
  #   </table>
  #
  def to_html_table(options = { no_header: false })
    options[:no_header] = false if options[:no_header].nil?
    cnt = options[:no_header] ? 1 : 0
    ret = reduce(['<table>']) do |rets, lines|
      ret = lines.reduce([]) do |ret, column|
        tag = (cnt == 0) ? 'th' : 'td'
        ret << "    <#{tag}>#{column}</#{tag}>"
        ret
      end
      cnt += 1
      inner_tr = ret.join("\n")
      rets << "  <tr>\n#{inner_tr}\n  </tr>"
    end.join("\n") + "\n</table>\n"
  end
end

Version data entries

61 entries across 61 versions & 1 rubygems

Version Path
tbpgr_utils-0.0.131 lib/open_classes/array/to_html_table.rb
tbpgr_utils-0.0.130 lib/open_classes/array/to_html_table.rb
tbpgr_utils-0.0.129 lib/open_classes/array/to_html_table.rb
tbpgr_utils-0.0.128 lib/open_classes/array/to_html_table.rb
tbpgr_utils-0.0.127 lib/open_classes/array/to_html_table.rb
tbpgr_utils-0.0.126 lib/open_classes/array/to_html_table.rb
tbpgr_utils-0.0.125 lib/open_classes/array/to_html_table.rb
tbpgr_utils-0.0.124 lib/open_classes/array/to_html_table.rb
tbpgr_utils-0.0.123 lib/open_classes/array/to_html_table.rb
tbpgr_utils-0.0.122 lib/open_classes/array/to_html_table.rb
tbpgr_utils-0.0.121 lib/open_classes/array/to_html_table.rb
tbpgr_utils-0.0.120 lib/open_classes/array/to_html_table.rb
tbpgr_utils-0.0.119 lib/open_classes/array/to_html_table.rb
tbpgr_utils-0.0.118 lib/open_classes/array/to_html_table.rb
tbpgr_utils-0.0.117 lib/open_classes/array/to_html_table.rb
tbpgr_utils-0.0.116 lib/open_classes/array/to_html_table.rb
tbpgr_utils-0.0.115 lib/open_classes/array/to_html_table.rb
tbpgr_utils-0.0.114 lib/open_classes/array/to_html_table.rb
tbpgr_utils-0.0.113 lib/open_classes/array/to_html_table.rb
tbpgr_utils-0.0.112 lib/open_classes/array/to_html_table.rb