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)


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

def depth
  unit.depth + 1
end

#inspect ⇒ Object



91
92
93
# File 'lib/unitwise/scale.rb', line 91

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

#inverse_scalar(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



67
68
69
# File 'lib/unitwise/scale.rb', line 67

def inverse_scalar(scalar = scalar)
  unit.inverse_scalar(scalar)
end

#root_terms ⇒ Array

The base terms this scale's unit is derived from

Returns:

  • (Array) —

    An array of Unitwise::Term



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

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



54
55
56
57
58
59
60
# File 'lib/unitwise/scale.rb', line 54

def scalar(magnitude = value)
  if special?
    unit.scalar(magnitude)
  else
    value * unit.scalar
  end
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.



87
88
89
# File 'lib/unitwise/scale.rb', line 87

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