Class: Turnip::Table
Defined Under Namespace
Classes: WidthMismatch
Instance Attribute Summary collapse
-
#raw ⇒ Object
(also: #to_a)
readonly
Returns the value of attribute raw.
Instance Method Summary collapse
- #each ⇒ Object
- #hashes ⇒ Object
- #headers ⇒ Object
-
#initialize(raw) ⇒ Table
constructor
A new instance of Table.
- #rows ⇒ Object
- #rows_hash ⇒ Object
- #transpose ⇒ Object
Constructor Details
#initialize(raw) ⇒ Table
Returns a new instance of Table.
8 9 10 |
# File 'lib/turnip/table.rb', line 8 def initialize(raw) @raw = raw end |
Instance Attribute Details
#raw ⇒ Object (readonly) Also known as: to_a
Returns the value of attribute raw.
3 4 5 |
# File 'lib/turnip/table.rb', line 3 def raw @raw end |
Instance Method Details
#each ⇒ Object
33 34 35 |
# File 'lib/turnip/table.rb', line 33 def each raw.each { |row| yield(row) } end |
#hashes ⇒ Object
20 21 22 |
# File 'lib/turnip/table.rb', line 20 def hashes rows.map { |row| Hash[headers.zip(row)] } end |
#headers ⇒ Object
12 13 14 |
# File 'lib/turnip/table.rb', line 12 def headers raw.first end |
#rows ⇒ Object
16 17 18 |
# File 'lib/turnip/table.rb', line 16 def rows raw.drop(1) end |
#rows_hash ⇒ Object
24 25 26 27 |
# File 'lib/turnip/table.rb', line 24 def rows_hash raise WidthMismatch.new(2, width) unless width == 2 transpose.hashes.first end |
#transpose ⇒ Object
29 30 31 |
# File 'lib/turnip/table.rb', line 29 def transpose self.class.new(raw.transpose) end |