Module: Unitwise::Composable
Class Method Summary collapse
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #composition ⇒ Object
- #composition_string ⇒ Object
- #dim ⇒ Object
- #similar_to?(other) ⇒ Boolean
Class Method Details
.included(base) ⇒ Object
4 5 6 |
# File 'lib/unitwise/composable.rb', line 4 def self.included(base) base.send :include, Comparable end |
Instance Method Details
#<=>(other) ⇒ Object
28 29 30 31 32 |
# File 'lib/unitwise/composable.rb', line 28 def <=>(other) if other.respond_to?(:composition) && similar_to?(other) scalar <=> other.scalar end end |
#composition ⇒ Object
8 9 10 11 12 |
# File 'lib/unitwise/composable.rb', line 8 def composition root_terms.reduce(SignedMultiset.new) do |s, t| s.increment(t.atom.dim, t.exponent) if t.atom; s end end |
#composition_string ⇒ Object
18 19 20 21 22 |
# File 'lib/unitwise/composable.rb', line 18 def composition_string composition.sort.map do |k,v| v == 1 ? k.to_s : "#{k}#{v}" end.join('.') end |
#dim ⇒ Object
14 15 16 |
# File 'lib/unitwise/composable.rb', line 14 def dim composition_string end |
#similar_to?(other) ⇒ Boolean
24 25 26 |
# File 'lib/unitwise/composable.rb', line 24 def similar_to?(other) self.composition == other.composition end |