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, #initialize

Instance Method Details

#atomsArray

List the atoms associated with this scale’s unit.

Returns:

  • (Array)


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

def atoms
  unit.atoms
end

#depthInteger

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

Returns:

  • (Integer)


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

def depth
  unit.depth + 1
end

#eql?(other) ⇒ Boolean

Redefine hash equality to match the hashes

Returns:

  • (Boolean)


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

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

#hashObject

Redefine hash for apropriate hash/key lookup



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

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

#inspectObject



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

def inspect
  "#<#{self.class} value=#{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



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

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



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

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



41
42
43
44
45
46
47
# File 'lib/unitwise/scale.rb', line 41

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)


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

def special?
  unit.special?
end

#termsArray

List the terms associated with this scale’s unit.

Returns:

  • (Array)


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

def terms
  unit.terms
end

#to_sObject

Convert to a simple string representing the scale.



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

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

#unit=(value) ⇒ Object

Set the unit vector.

Parameters:



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

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