Class: Nelson::MultipicationExpression

Inherits:
Expression
  • Object
show all
Defined in:
lib/nelson/expressions/multipication_expression.rb

Instance Attribute Summary

Attributes inherited from Expression

#terms

Instance Method Summary collapse

Methods inherited from Expression

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

Constructor Details

This class inherits a constructor from Nelson::Expression

Instance Method Details

#callObject



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

def call
  terms.reduce(:*)
end

#to_sObject



8
9
10
11
12
13
14
15
16
17
# File 'lib/nelson/expressions/multipication_expression.rb', line 8

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