Module: Unitwise::Compatible
Overview
Compatible is used to establish compatibility between units, terms, or measurements. This is done by determining the objects atomic composition represented as a Signed Multiset.
Class Method Summary collapse
- .included(base) ⇒ Object private
Instance Method Summary collapse
-
#<=>(other) ⇒ -1 0 1
Compare whether the instance is greater, less than or equal to other.
-
#compatible_with?(other) ⇒ true false
Determine if this instance is similar to or compatible with other.
-
#composition ⇒ SignedMultiset
A representation of a unit based on the atoms it's derived from.
-
#composition_string ⇒ String
A string representation of a unit based on the atoms it's derived from.
-
#dim ⇒ String
Define a default #dim for included classes.
Class Method Details
.included(base) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
8 9 10 |
# File 'lib/unitwise/compatible.rb', line 8 def self.included(base) base.send :include, Comparable end |
Instance Method Details
#<=>(other) ⇒ -1 0 1
Compare whether the instance is greater, less than or equal to other.
47 48 49 50 51 |
# File 'lib/unitwise/compatible.rb', line 47 def <=>(other) if other.respond_to?(:composition) && compatible_with?(other) scalar <=> other.scalar end end |
#compatible_with?(other) ⇒ true false
Determine if this instance is similar to or compatible with other
40 41 42 |
# File 'lib/unitwise/compatible.rb', line 40 def compatible_with?(other) self.composition == other.composition end |
#composition ⇒ SignedMultiset
A representation of a unit based on the atoms it's derived from.
15 16 17 18 19 |
# File 'lib/unitwise/compatible.rb', line 15 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 ⇒ String
A string representation of a unit based on the atoms it's derived from
31 32 33 34 35 |
# File 'lib/unitwise/compatible.rb', line 31 def composition_string composition.sort.map do |k,v| v == 1 ? k.to_s : "#{k}#{v}" end.join('.') end |
#dim ⇒ String
Define a default #dim for included classes.
24 25 26 |
# File 'lib/unitwise/compatible.rb', line 24 def dim composition_string end |