Class: Datasets::Table::Record

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/datasets/table.rb

Instance Method Summary collapse

Constructor Details

#initialize(table, index) ⇒ Record

Returns a new instance of Record.



8
9
10
11
# File 'lib/datasets/table.rb', line 8

def initialize(table, index)
  @table = table
  @index = index
end

Instance Method Details

#[](column_name_or_column_index) ⇒ Object



13
14
15
# File 'lib/datasets/table.rb', line 13

def [](column_name_or_column_index)
  @table[column_name_or_column_index][@index]
end

#eachObject



17
18
19
20
21
22
# File 'lib/datasets/table.rb', line 17

def each
  return to_enum(__method__) unless block_given?
  @table.each_column.each do |column_name, column_values|
    yield(column_name, column_values[@index])
  end
end

#inspectObject



38
39
40
# File 'lib/datasets/table.rb', line 38

def inspect
  "#<#{self.class.name} #{@table.dataset..name}[#{@index}] #{to_h.inspect}>"
end

#to_hObject



30
31
32
33
34
35
36
# File 'lib/datasets/table.rb', line 30

def to_h
  hash = {}
  each do |column_name, column_value|
    hash[column_name] = column_value
  end
  hash
end

#valuesObject



24
25
26
27
28
# File 'lib/datasets/table.rb', line 24

def values
  @table.each_column.collect do |_column_name, column_values|
    column_values[@index]
  end
end