Class: Calcexam::Matrix
- Inherits:
-
Object
- Object
- Calcexam::Matrix
- Includes:
- Enumerable
- Defined in:
- lib/calcexam/matrix.rb
Instance Attribute Summary collapse
-
#operation ⇒ Object
readonly
Returns the value of attribute operation.
-
#x ⇒ Object
readonly
Returns the value of attribute x.
-
#y ⇒ Object
readonly
Returns the value of attribute y.
Instance Method Summary collapse
- #[](i, j = nil) ⇒ Object
- #each ⇒ Object
-
#initialize(operation = :*, x = [], y = []) ⇒ Matrix
constructor
A new instance of Matrix.
- #shuffle! ⇒ Object
- #size ⇒ Object
- #unshuffle! ⇒ Object
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
#operation ⇒ Object (readonly)
Returns the value of attribute operation.
4 5 6 |
# File 'lib/calcexam/matrix.rb', line 4 def operation @operation end |
#x ⇒ Object (readonly)
Returns the value of attribute x.
4 5 6 |
# File 'lib/calcexam/matrix.rb', line 4 def x @x end |
#y ⇒ Object (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 |
#each ⇒ Object
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 |
#size ⇒ Object
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 |