Class: TSV::Row
Defined Under Namespace
Classes: InputError, InvalidKey, UnknownKey
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#header ⇒ Object
readonly
Returns the value of attribute header.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
-
#initialize(data, header) ⇒ Row
constructor
A new instance of Row.
- #with_header ⇒ Object (also: #to_h)
Constructor Details
#initialize(data, header) ⇒ Row
Returns a new instance of Row.
27 28 29 30 31 32 |
# File 'lib/tsv/row.rb', line 27 def initialize(data, header) @data = data @header = header raise InputError.new("Row has #{@data.length} columns, but #{@header.length} columns expected") if @data.length != @header.length end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
7 8 9 |
# File 'lib/tsv/row.rb', line 7 def data @data end |
#header ⇒ Object (readonly)
Returns the value of attribute header.
7 8 9 |
# File 'lib/tsv/row.rb', line 7 def header @header end |
Instance Method Details
#==(other) ⇒ Object
39 40 41 42 43 |
# File 'lib/tsv/row.rb', line 39 def ==(other) other.is_a?(self.class) and header == other.header and data == other.data end |
#[](key) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/tsv/row.rb', line 13 def [](key) if key.is_a? ::String raise UnknownKey unless header.include?(key) data[header.index(key)] elsif key.is_a? ::Numeric raise UnknownKey if data[key].nil? data[key] else raise InvalidKey.new end end |
#[]=(key, value) ⇒ Object
9 10 11 |
# File 'lib/tsv/row.rb', line 9 def []=(key, value) raise TSV::ReadOnly.new('TSV data is read only. Export data to modify it.') end |
#with_header ⇒ Object Also known as: to_h
34 35 36 |
# File 'lib/tsv/row.rb', line 34 def with_header Hash[header.zip(data)] end |