Class: Calcexam::Matrix

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/calcexam/matrix.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(operation = :*, x = [], y = []) ⇒ Matrix

Returns a new instance of Matrix.



6
7
8
9
# File 'lib/calcexam/matrix.rb', line 6

def initialize(operation=:*, x=[], y=[])
  @operation = operation
  @x, @y = parse_interval(x), parse_interval(y)
end

Instance Attribute Details

#operationObject (readonly)

Returns the value of attribute operation.



4
5
6
# File 'lib/calcexam/matrix.rb', line 4

def operation
  @operation
end

#xObject (readonly)

Returns the value of attribute x.



4
5
6
# File 'lib/calcexam/matrix.rb', line 4

def x
  @x
end

#yObject (readonly)

Returns the value of attribute y.



4
5
6
# File 'lib/calcexam/matrix.rb', line 4

def y
  @y
end

Instance Method Details

#[](i, j = nil) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/calcexam/matrix.rb', line 29

def [](i, j=nil)
  if j.nil?
    j = i / @x.size
    i = i % @x.size
  end
  [@x[i], @y[j]]
end

#eachObject



11
12
13
14
15
16
17
# File 'lib/calcexam/matrix.rb', line 11

def each
  @x.each do |x|
    @y.each do |y|
      yield([x, y])
    end
  end
end

#shuffle!Object



19
20
21
22
23
# File 'lib/calcexam/matrix.rb', line 19

def shuffle!
  @x.shuffle!
  @y.shuffle!
  self
end

#sizeObject



25
26
27
# File 'lib/calcexam/matrix.rb', line 25

def size
  @x.size * @y.size
end