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(*args) ⇒ Scale

Returns a new instance of Scale.



10
11
12
13
# File 'lib/unitwise/scale.rb', line 10

def initialize(*args)
  super(*args)
  freeze
end

Instance Method Details

#atoms ⇒ Array

List the atoms associated with this scale's unit.

Returns:

  • (Array)


25
26
27
# File 'lib/unitwise/scale.rb', line 25

def atoms
  unit.atoms
end

#depth ⇒ Integer

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

Returns:

  • (Integer)


71
72
73
# File 'lib/unitwise/scale.rb', line 71

def depth
  unit.depth + 1
end

#functional(x = value, forward = true) ⇒ Numeric

Return a converted value for this scale, based on it's function for scales with special units.

Parameters:

  • x (Numeric) (defaults to: value) —

    Value to convert to or from

  • forward (true, false) (defaults to: true) —

    whether to convert to this unit or from it.

Returns:



49
50
51
# File 'lib/unitwise/scale.rb', line 49

def functional(x=value, forward=true)
  unit.functional(x, forward)
end

#inspect ⇒ Object



81
82
83
# File 'lib/unitwise/scale.rb', line 81

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

#root_terms ⇒ Array

The base units this scale's unit is derived from

Returns:

  • (Array) —

    An array of Unitwise::Term



64
65
66
# File 'lib/unitwise/scale.rb', line 64

def root_terms
  unit.root_terms
end

#scalar ⇒ Numeric

Return a scalar value for non-special units, this will be some ratio of a child base unit.

Returns:



57
58
59
# File 'lib/unitwise/scale.rb', line 57

def scalar
  value * unit.scalar
end

#special? ⇒ true, false

Is this scale's unit special?

Returns:

  • (true, false)


39
40
41
# File 'lib/unitwise/scale.rb', line 39

def special?
  unit.special?
end

#terms ⇒ Array

List the terms associated with this scale's unit.

Returns:

  • (Array)


32
33
34
# File 'lib/unitwise/scale.rb', line 32

def terms
  unit.terms
end

#to_s ⇒ Object

Convert to a simple string representing the scale.



77
78
79
# File 'lib/unitwise/scale.rb', line 77

def to_s
  "#{value} #{unit}"
end

#unit ⇒ Unitwise::Unit

The unit associated with this scale.

Returns:



18
19
20
# File 'lib/unitwise/scale.rb', line 18

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