Sha256: eac74a330e8367c2a3e1ee82153df1efa47802ad7b3ec5ae0744e5e9e30e26e0
Contents?: true
Size: 1.59 KB
Versions: 19
Compression:
Stored size: 1.59 KB
Contents
module Unitwise # 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. module Compatible # @api private def self.included(base) base.send :include, Comparable base.send :include, Memoizable unless base < Memoizable base.send :memoize, :composition, :composition_string end def initialize(*args) super(*args) freeze end # A representation of a unit based on the atoms it's derived from. # @return [SignedMultiset] # @api public def composition root_terms.reduce(SignedMultiset.new) do |s, t| s.increment(t.atom.dim, t.exponent) if t.atom s end end # Define a default #dim for included classes. # @return [String] # @api public def dim composition_string end # A string representation of a unit based on the atoms it's derived from # @return [String] # @api public def composition_string composition.sort.map do |k, v| v == 1 ? k.to_s : "#{k}#{v}" end.join('.') end # Determine if this instance is similar to or compatible with other # @return [true false] # @api public def compatible_with?(other) composition == other.composition end # Compare whether the instance is greater, less than or equal to other. # @return [-1 0 1] # @api public def <=>(other) if other.respond_to?(:composition) && compatible_with?(other) scalar <=> other.scalar end end end end
Version data entries
19 entries across 19 versions & 2 rubygems