Class: Octave::StructMatrix

Inherits:
Matrix
  • Object
show all
Defined in:
lib/octave/matrix.rb

Overview

The StructMatrix class is used for storing hash values that are sent to or received from Octave struct matrices. Usage:

require 'octave'

struct_matrix = Octave::StructMatrix.new(17, 1, "name", "age", "married"
17.times do |m|
  struct_matrix[m, 0]["name"] = "Bob #{m}"
  struct_matrix[m, 0]["age"] = (rand * 100).to_i
  struct_matrix[m, 0]["married"] = (rand > 0.5)
end

p struct_matrix[16, 0]

Instance Attribute Summary collapse

Attributes inherited from Matrix

#cells, #m, #n

Instance Method Summary collapse

Methods inherited from Matrix

#==, #[], #[]=, #to_a

Constructor Details

#initialize(m, n, *names) ⇒ StructMatrix

Creates a new StructMatrix with the given dimensions for row and column size and the names of the attributes



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/octave/matrix.rb', line 77

def initialize(m, n, *names)
  super(m, n)
  @names = names
  
  # Populate the matrix with a hash using the names provided
  m.times do |row_index|
    n.times do |column_index|
      @cells[row_index][column_index] = names.inject({}) { |s,e| s.merge( { e.to_s => nil } ) }
    end
  end
end

Instance Attribute Details

#namesObject (readonly)

Returns the value of attribute names.



73
74
75
# File 'lib/octave/matrix.rb', line 73

def names
  @names
end