Class: Unitwise::Expression::Composer
- Inherits:
-
Object
- Object
- Unitwise::Expression::Composer
- Defined in:
- lib/unitwise/expression/composer.rb
Instance Attribute Summary collapse
-
#terms ⇒ Object
readonly
Returns the value of attribute terms.
Instance Method Summary collapse
- #denominator ⇒ Object
- #expression ⇒ Object
-
#initialize(terms) ⇒ Composer
constructor
A new instance of Composer.
- #numerator ⇒ Object
- #set ⇒ Object
Constructor Details
#initialize(terms) ⇒ Composer
Returns a new instance of Composer.
5 6 7 |
# File 'lib/unitwise/expression/composer.rb', line 5 def initialize(terms) @terms = terms end |
Instance Attribute Details
#terms ⇒ Object (readonly)
Returns the value of attribute terms.
4 5 6 |
# File 'lib/unitwise/expression/composer.rb', line 4 def terms @terms end |
Instance Method Details
#denominator ⇒ Object
21 22 23 24 25 |
# File 'lib/unitwise/expression/composer.rb', line 21 def denominator @denominator ||= set.select{|k,v| v < 0}.map do |k,v| "#{k[:f] if k[:f] != 1}#{k[:p]}#{k[:a]}#{-v if v != -1}" end.select{|t| !t.empty?}.join('.') end |
#expression ⇒ Object
27 28 29 30 31 32 |
# File 'lib/unitwise/expression/composer.rb', line 27 def expression @expression = [] @expression << (numerator.empty? ? '1' : numerator) (@expression << denominator) unless denominator.empty? @expression.join('/') end |
#numerator ⇒ Object
15 16 17 18 19 |
# File 'lib/unitwise/expression/composer.rb', line 15 def numerator @numerator ||= set.select{|k,v| v > 0}.map do |k,v| "#{k[:f] if k[:f] != 1}#{k[:p]}#{k[:a]}#{v if v != 1}" end.select{|t| !t.empty?}.join('.') end |
#set ⇒ Object
9 10 11 12 13 |
# File 'lib/unitwise/expression/composer.rb', line 9 def set @set ||= terms.reduce(SignedMultiset.new) do |s, t| s.increment({:f => t.factor, :p => t.prefix, :a => t.atom}, t.exponent); s end end |