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
10
# File 'lib/calcexam/matrix.rb', line 6

def initialize(operation=:*, x=[], y=[])
  @operation = operation
  @x, @y = parse_interval(x), parse_interval(y)
  @do_shuffle = false
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



34
35
36
37
38
39
40
# File 'lib/calcexam/matrix.rb', line 34

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

#eachObject



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

def each
  idxs = (0..(@x.size * @y.size - 1).to_i).to_a
  idxs.shuffle! if @do_shuffle
  idxs.each do |idx|
    yield self[idx]
  end
end

#shuffle!Object



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

def shuffle!
  @do_shuffle = true
  self
end

#sizeObject



30
31
32
# File 'lib/calcexam/matrix.rb', line 30

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

#unshuffle!Object



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

def unshuffle!
  @do_shuffle = false
  self
end