Class: Nelson::DivisionExpression

Inherits:
Expression show all
Defined in:
lib/nelson/expressions/division_expression.rb

Instance Attribute Summary

Attributes inherited from Expression

#terms

Instance Method Summary collapse

Methods inherited from Expression

#*, #+, #-, #/, #has_term?

Constructor Details

#initialize(term1, term2) ⇒ DivisionExpression

Returns a new instance of DivisionExpression.



4
5
6
# File 'lib/nelson/expressions/division_expression.rb', line 4

def initialize(term1, term2)
  super(term1, term2)
end

Instance Method Details

#callObject



8
9
10
# File 'lib/nelson/expressions/division_expression.rb', line 8

def call
  terms.map { |n| n.is_a?(Numeric) ? n.to_f : n }.reduce(:/)
end

#to_sObject



12
13
14
15
16
17
18
19
20
21
# File 'lib/nelson/expressions/division_expression.rb', line 12

def to_s
  terms.sort_by { |t| t.is_a?(Expression) ? 1 : 0 }.reduce("") do |c, t|
    case t
    when Expression
      c << "(#{t})"
    else
      c = [c, t].map(&:to_s).reject(&:empty?).join("/")
    end
  end
end