Class: Nelson::DivisionExpression
- Inherits:
-
Expression
- Object
- Expression
- Nelson::DivisionExpression
- Defined in:
- lib/nelson/expressions/division_expression.rb
Instance Attribute Summary
Attributes inherited from Expression
Instance Method Summary collapse
- #call ⇒ Object
-
#initialize(term1, term2) ⇒ DivisionExpression
constructor
A new instance of DivisionExpression.
- #to_s ⇒ Object
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
#call ⇒ Object
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_s ⇒ Object
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 |