Class: Unitwise::Scale

Inherits:
Object
  • Object
show all
Includes:
Compatible
Defined in:
lib/unitwise/scale.rb

Overview

A Unitwise::Scale represents a value and a unit, sort of like a vector, it has two components. In this case, it's a value and unit rather than a magnitude and direction. This class should be considered mostly privateish.

Direct Known Subclasses

Functional, Measurement

Instance Method Summary collapse

Methods included from Compatible

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

Constructor Details

#initialize(value, unit) ⇒ Scale

Returns a new instance of Scale



9
10
11
12
13
14
15
16
17
# File 'lib/unitwise/scale.rb', line 9

def initialize(value, unit)
  self.value = if value.is_a? self.class
    value.convert_to(unit).value
  else
    value
  end
  self.unit = unit
  freeze
end

Instance Method Details

#atomsArray

List the atoms associated with this scale's unit.

Returns:

  • (Array)


29
30
31
# File 'lib/unitwise/scale.rb', line 29

def atoms
  unit.atoms
end

#depthInteger

How far away is this instances unit from the deepest level atom.

Returns:

  • (Integer)


83
84
85
# File 'lib/unitwise/scale.rb', line 83

def depth
  unit.depth + 1
end

#eql?(other) ⇒ Boolean

Redefine hash equality to match the hashes

Returns:

  • (Boolean)


126
127
128
# File 'lib/unitwise/scale.rb', line 126

def eql?(other)
  hash == other.hash
end

#expressionObject



98
99
100
# File 'lib/unitwise/scale.rb', line 98

def expression
  unit.expression
end

#hashObject

Redefine hash for apropriate hash/key lookup



119
120
121
# File 'lib/unitwise/scale.rb', line 119

def hash
  [value, unit.to_s, self.class].hash
end

#inspectObject



113
114
115
# File 'lib/unitwise/scale.rb', line 113

def inspect
  "#<#{self.class} value=#{simplified_value} unit=#{unit}>"
end

#magnitude(scalar = scalar()) ⇒ Numeric

Get a magnitude based on a linear scale value. Only used by scales with special atoms in it's hierarchy.

Parameters:

  • scalar (Numeric) (defaults to: scalar())

    A linear scalar value

Returns:

  • (Numeric)

    The equivalent magnitude on this scale



64
65
66
67
68
69
70
# File 'lib/unitwise/scale.rb', line 64

def magnitude(scalar = scalar())
  if special?
    unit.magnitude(scalar)
  else
    value * unit.magnitude
  end
end

#root_termsArray

The base terms this scale's unit is derived from

Returns:

  • (Array)

    An array of Unitwise::Term



75
76
77
# File 'lib/unitwise/scale.rb', line 75

def root_terms
  unit.root_terms
end

#scalar(magnitude = value) ⇒ Numeric

Get a scalar value for this scale.

Parameters:

  • magnitude (Numeric) (defaults to: value)

    An optional magnitude on this scale.

Returns:

  • (Numeric)

    A scalar value on a linear scale



51
52
53
54
55
56
57
# File 'lib/unitwise/scale.rb', line 51

def scalar(magnitude = value)
  if special?
    unit.scalar(magnitude)
  else
    Number.rationalize(value) * Number.rationalize(unit.scalar)
  end
end

#simplified_valueNumeric

Attempts to coerce the value to the simplest Numeric that fully expresses it's value. For instance a value of 1.0 would return 1, a value of #<BigDecimal:7f9558d559b8,'0.45E1',18(18)> would return 4.5.

Returns:

  • (Numeric)


93
94
95
# File 'lib/unitwise/scale.rb', line 93

def simplified_value
  Unitwise::Number.simplify(value)
end

#special?true, false

Is this scale's unit special?

Returns:

  • (true, false)


43
44
45
# File 'lib/unitwise/scale.rb', line 43

def special?
  unit.special?
end

#termsArray

List the terms associated with this scale's unit.

Returns:

  • (Array)


36
37
38
# File 'lib/unitwise/scale.rb', line 36

def terms
  unit.terms
end

#to_s(mode = nil) ⇒ Object

Convert to a simple string representing the scale.



104
105
106
107
108
109
110
111
# File 'lib/unitwise/scale.rb', line 104

def to_s(mode = nil)
  unit_string = unit.to_s(mode)
  if unit_string && unit_string != '1'
    "#{simplified_value} #{unit_string}"
  else
    simplified_value.to_s
  end
end

#unit=(value) ⇒ Object

Set the unit vector.

Parameters:



22
23
24
# File 'lib/unitwise/scale.rb', line 22

def unit=(value)
  @unit = value.is_a?(Unit) ? value : Unit.new(value)
end