Class: Unitwise::Expression::Composer
- Inherits:
-
Object
- Object
- Unitwise::Expression::Composer
- Defined in:
- lib/unitwise/expression/composer.rb
Instance Attribute Summary collapse
-
#mode ⇒ Object
readonly
Returns the value of attribute mode.
-
#terms ⇒ Object
readonly
Returns the value of attribute terms.
Instance Method Summary collapse
- #denominator ⇒ Object
- #expression ⇒ Object
-
#initialize(terms, mode) ⇒ Composer
constructor
A new instance of Composer.
- #numerator ⇒ Object
- #set ⇒ Object
Constructor Details
#initialize(terms, mode) ⇒ Composer
Returns a new instance of Composer.
5 6 7 8 |
# File 'lib/unitwise/expression/composer.rb', line 5 def initialize(terms, mode) @terms = terms @mode = mode || :primary_code end |
Instance Attribute Details
#mode ⇒ Object (readonly)
Returns the value of attribute mode.
4 5 6 |
# File 'lib/unitwise/expression/composer.rb', line 4 def mode @mode end |
#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
25 26 27 28 29 |
# File 'lib/unitwise/expression/composer.rb', line 25 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
31 32 33 34 35 36 |
# File 'lib/unitwise/expression/composer.rb', line 31 def expression @expression = [] @expression << (numerator.empty? ? '1' : numerator) (@expression << denominator) unless denominator.empty? @expression.join('/') end |
#numerator ⇒ Object
19 20 21 22 23 |
# File 'lib/unitwise/expression/composer.rb', line 19 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
10 11 12 13 14 15 16 17 |
# File 'lib/unitwise/expression/composer.rb', line 10 def set @set ||= terms.reduce(SignedMultiset.new) do |s, t| identifier = { :f => t.factor, :p => (t.prefix.to_s(mode) if t.prefix), :a => (t.atom.to_s(mode) if t.atom) } s.increment(identifier, t.exponent); s end end |