lib/unitwise/unit.rb in unitwise-0.6.0 vs lib/unitwise/unit.rb in unitwise-0.6.1

- old
+ new

@@ -8,16 +8,17 @@ # Create a new unit. You can send an expression or a collection of terms # @param input [String, Unit, [Term]] A string expression, a unit, or a # collection of tems. def initialize(input) - if input.respond_to?(:expression) + case input + when Compatible @expression = input.expression - elsif input.respond_to?(:each) - @terms = input - else + when String, Symbol @expression = input.to_s + else + @terms = input end end def expression @expression ||= (Expression.compose(@terms) if @terms) @@ -33,27 +34,26 @@ def special? terms.count == 1 && terms.all?(&:special?) end - def depth terms.map(&:depth).max + 1 end def root_terms - terms.flat_map(&:root_terms) + terms.map(&:root_terms).flatten end - def scalar(x = 1) + def scalar(magnitude = 1) terms.reduce(1) do |prod, term| - prod * term.scalar(x) + prod * term.scalar(magnitude) end end - def inverse_scalar(x = 1) + def magnitude(scalar = scalar) terms.reduce(1) do |prod, term| - prod * term.inverse_scalar(x) + prod * term.magnitude(scalar) end end def *(other) if other.respond_to?(:terms)