Class: DataTable

Inherits:
Object
  • Object
show all
Defined in:
lib/source/repositories/containers/data_table.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table) ⇒ DataTable

Returns a new instance of DataTable.



7
8
9
10
11
12
13
14
15
16
# File 'lib/source/repositories/containers/data_table.rb', line 7

def initialize(table)

  self.rows_count = table.length
  max_cols = 0
  table.each do |row|
    max_cols = row.length if row.size > max_cols
  end
  self.cols_count = max_cols
  self.table = table
end

Instance Attribute Details

#cols_countObject

Returns the value of attribute cols_count.



5
6
7
# File 'lib/source/repositories/containers/data_table.rb', line 5

def cols_count
  @cols_count
end

#rows_countObject

Returns the value of attribute rows_count.



5
6
7
# File 'lib/source/repositories/containers/data_table.rb', line 5

def rows_count
  @rows_count
end

Instance Method Details

#get_item(row, col) ⇒ Object



18
19
20
21
22
23
# File 'lib/source/repositories/containers/data_table.rb', line 18

def get_item(row, col)
  return nil if row >= rows_count || row.negative?
  return nil if col >= cols_count || col.negative?

  table[row][col]
end

#to_my_arrayObject



25
26
27
# File 'lib/source/repositories/containers/data_table.rb', line 25

def to_my_array
  table.dup
end

#to_sObject



29
30
31
# File 'lib/source/repositories/containers/data_table.rb', line 29

def to_s
  table.map { |row| "[#{row.join(', ')}]" }.join("\n")
end