Class: Unitwise::Measurement

Inherits:
Scale
  • Object
show all
Defined in:
lib/unitwise/measurement.rb

Overview

A Measurement is a combination of a numeric value and a unit. You can think of this as a type of vector where the direction is the unit designation and the value is the magnitued. This is the primary class that outside code will interact with. Comes with conversion, comparison, and math methods.

Instance Method Summary collapse

Methods inherited from Scale

#atoms, #depth, #functional, #inspect, #root_terms, #scalar, #special?, #terms, #to_s, #unit

Methods included from Compatible

#<=>, #compatible_with?, #composition, #composition_string, #dim, included

Constructor Details

#initialize(*args) ⇒ Measurement

Create a new Measurement Measurement::Unit

Examples:

Unitwise::Measurement.new(20, 'm/s') # => #<Unitwise::Measurement 20 m/s>

Parameters:

  • value (Numeric) —

    The scalar value for the measurement

  • unit (String, Measurement::Unit) —

    Either a string expression, or a



15
16
17
18
19
20
# File 'lib/unitwise/measurement.rb', line 15

def initialize(*args)
  super(*args)
  if terms.nil?
    raise ExpressionError, "Could not evaluate `#{unit}`."
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object

Will attempt to convert to a unit by method name.

Examples:

measurement.to_foot # => <Unitwise::Measurement 4 foot>


133
134
135
136
137
138
139
140
141
142
143
# File 'lib/unitwise/measurement.rb', line 133

def method_missing(meth, *args, &block)
  if args.empty? && !block_given? && (match = /\Ato_(\w+)\Z/.match(meth))
    begin
      convert_to(match[1])
    rescue ExpressionError
      super(meth, *args, &block)
    end
  else
    super(meth, *args, &block)
  end
end

Instance Method Details

#*(other) ⇒ Object

Multiply this measurement by a number or another measurement

Examples:

measurent * 5
measurement * some_other_measurement

Parameters:



44
45
46
# File 'lib/unitwise/measurement.rb', line 44

def *(other)
  operate(:*, other) || raise(TypeError, "Can't multiply #{self} by #{other}.")
end

#**(number) ⇒ Object

Raise a measurement to a numeric power.

Examples:

measurement ** 2

Parameters:



81
82
83
84
85
86
87
# File 'lib/unitwise/measurement.rb', line 81

def **(number)
  if number.is_a?(Numeric)
    new( value ** number, unit ** number )
  else
    raise TypeError, "Can't raise #{self} to #{number} power."
  end
end

#+(other) ⇒ Object

Add another measurement to this unit. Units must be compatible.

Examples:

measurement + some_other_measurement

Parameters:



63
64
65
# File 'lib/unitwise/measurement.rb', line 63

def +(other)
  combine(:+, other) || raise(TypeError, "Can't add #{other} to #{self}.")
end

#-(other) ⇒ Object

Subtract another measurement from this unit. Units must be compatible.

Examples:

measurement - some_other_measurement

Parameters:



72
73
74
# File 'lib/unitwise/measurement.rb', line 72

def -(other)
  combine(:-, other) || raise(TypeError, "Can't subtract #{other} from #{self}.")
end

#/(other) ⇒ Object

Divide this measurement by a number or another measurement

Examples:

measurement / 2
measurement / some_other_measurement

Parameters:



54
55
56
# File 'lib/unitwise/measurement.rb', line 54

def /(other)
  operate(:/, other) || raise(TypeError, "Can't divide #{self} by #{other}")
end

#coerce(other) ⇒ Object

Coerce a numeric to a a measurement for mathematical operations

Examples:

2.5 * measurement
4 / measurement

Parameters:



95
96
97
98
99
100
101
102
# File 'lib/unitwise/measurement.rb', line 95

def coerce(other)
  case other
  when Numeric
    return self.class.new(other, '1'), self
  else
    raise TypeError, "#{self.class} can't be coerced into #{other.class}"
  end
end

#convert_to(other_unit) ⇒ Object

Convert this measurement to a compatible unit. or a Measurement::Unit

Examples:

measurement1.convert_to('foot')
measurement2.convert_to('kilogram')

Parameters:

  • other_unit (String, Measurement::Unit) —

    Either a string expression



29
30
31
32
33
34
35
36
# File 'lib/unitwise/measurement.rb', line 29

def convert_to(other_unit)
  other_unit = Unit.new(other_unit)
  if compatible_with?(other_unit)
    new(converted_value(other_unit), other_unit)
  else
    raise ConversionError, "Can't convert #{self} to #{other_unit}."
  end
end

#to_f ⇒ Object

Convert a measurement to a Float.

Examples:

measurement.to_f # => 4.25


117
118
119
# File 'lib/unitwise/measurement.rb', line 117

def to_f
  Float(value)
end

#to_i ⇒ Object Also known as: to_int

Convert a measurement to an Integer.

Examples:

measurement.to_i # => 4


108
109
110
# File 'lib/unitwise/measurement.rb', line 108

def to_i
  Integer(value)
end

#to_r ⇒ Object

Convert a measurement to a Rational.

Examples:

measurement.to_r # => (17/4)


125
126
127
# File 'lib/unitwise/measurement.rb', line 125

def to_r
  Rational(value)
end