Class: Fractify::Operator

Inherits:
Object
  • Object
show all
Defined in:
lib/fractify/operator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(operator_character, rank, to_left = nil, to_right = nil) ⇒ Operator

Returns a new instance of Operator.



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/fractify/operator.rb', line 8

def initialize(operator_character, rank, to_left = nil, to_right = nil)
  if to_left && !to_left.is_a?(Fractify::Fraction) || to_right && !to_right.is_a?(Fractify::Fraction)
    raise IncorrectArgumentsError
  end

  self.operator_character = operator_character
  self.rank = rank
  self.to_left = to_left
  self.to_right = to_right
  self.executed = false
end

Instance Attribute Details

#executedObject

Returns the value of attribute executed.



6
7
8
# File 'lib/fractify/operator.rb', line 6

def executed
  @executed
end

#operator_characterObject

Returns the value of attribute operator_character.



6
7
8
# File 'lib/fractify/operator.rb', line 6

def operator_character
  @operator_character
end

#rankObject

Returns the value of attribute rank.



6
7
8
# File 'lib/fractify/operator.rb', line 6

def rank
  @rank
end

#to_leftObject

Returns the value of attribute to_left.



6
7
8
# File 'lib/fractify/operator.rb', line 6

def to_left
  @to_left
end

#to_rightObject

Returns the value of attribute to_right.



6
7
8
# File 'lib/fractify/operator.rb', line 6

def to_right
  @to_right
end

Instance Method Details

#<=>(other) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/fractify/operator.rb', line 20

def <=>(other)
  raise IncorrectArgumentsError unless other.is_a? Fractify::Operator
  return 0 if rank == other.rank
  return 1 if rank > other.rank

  -1
end

#decrease_rank!(number = 1) ⇒ Object



32
33
34
# File 'lib/fractify/operator.rb', line 32

def decrease_rank!(number = 1)
  @rank -= number
end

#executed!Object



40
41
42
# File 'lib/fractify/operator.rb', line 40

def executed!
  self.executed = true
end

#executed?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/fractify/operator.rb', line 44

def executed?
  executed
end

#increase_rank!(number = 1) ⇒ Object



36
37
38
# File 'lib/fractify/operator.rb', line 36

def increase_rank!(number = 1)
  @rank += number
end

#not_executed?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/fractify/operator.rb', line 48

def not_executed?
  !executed
end

#to_sObject



28
29
30
# File 'lib/fractify/operator.rb', line 28

def to_s
  "(#{operator_character}, #{rank})"
end