Class: RubyUnits::Unit::Definition

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_units/definition.rb

Overview

Handle the definition of units

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, definition = []) {|_self| ... } ⇒ Definition

Returns a new instance of Definition.

Examples:

Raw definition from a hash

Unit::Definition.new("rack-unit",[%w{U rack-U}, (6405920109971793/144115188075855872), :length, %w{<meter>} ])

Block form

Unit::Definition.new("rack-unit") do |unit|
  unit.aliases = %w{U rack-U}
  unit.definition = RubyUnits::Unit.new("7/4 inches")
end

Yields:

  • (_self)

Yield Parameters:



35
36
37
38
39
40
41
42
43
44
# File 'lib/ruby_units/definition.rb', line 35

def initialize(name, definition = [])
  yield self if block_given?
  self.name     ||= name.gsub(/[<>]/, '')
  @aliases      ||= (definition[0] || [name])
  @scalar       ||= definition[1]
  @kind         ||= definition[2]
  @numerator    ||= definition[3] || RubyUnits::Unit::UNITY_ARRAY
  @denominator  ||= definition[4] || RubyUnits::Unit::UNITY_ARRAY
  @display_name ||= @aliases.first
end

Instance Attribute Details

#aliasesArray

alias array must contain the name of the unit and entries must be unique

Returns:



63
64
65
# File 'lib/ruby_units/definition.rb', line 63

def aliases
  [[@aliases], @name, @display_name].flatten.compact.uniq
end

#denominatorArray

Returns:



17
18
19
# File 'lib/ruby_units/definition.rb', line 17

def denominator
  @denominator
end

#display_nameString

Unit name to be used when generating output. This MUST be a parseable string or it won’t be possible to round trip the unit to a String and back.

Returns:



24
25
26
# File 'lib/ruby_units/definition.rb', line 24

def display_name
  @display_name
end

#kindSymbol

Returns:

  • (Symbol)


8
9
10
# File 'lib/ruby_units/definition.rb', line 8

def kind
  @kind
end

#numeratorArray

Returns:



14
15
16
# File 'lib/ruby_units/definition.rb', line 14

def numerator
  @numerator
end

#scalarNumeric

Returns:



11
12
13
# File 'lib/ruby_units/definition.rb', line 11

def scalar
  @scalar
end

Instance Method Details

#base?Boolean

is this a base unit? units are base units if the scalar is one, and the unit is defined in terms of itself.

Returns:

  • (Boolean)


94
95
96
97
98
99
100
# File 'lib/ruby_units/definition.rb', line 94

def base?
  (denominator     == RubyUnits::Unit::UNITY_ARRAY) &&
    (numerator       != RubyUnits::Unit::UNITY_ARRAY) &&
    (numerator.size  == 1) &&
    (scalar          == 1) &&
    (numerator.first == self.name)
end

#definition=(unit) ⇒ Unit::Definition

define a unit in terms of another unit

Parameters:

Returns:



70
71
72
73
74
75
76
77
# File 'lib/ruby_units/definition.rb', line 70

def definition=(unit)
  base         = unit.to_base
  @scalar      = base.scalar
  @kind        = base.kind
  @numerator   = base.numerator
  @denominator = base.denominator
  self
end

#nameString?

TODO:

refactor Unit and Unit::Definition so we don’t need to wrap units with angle brackets

name of the unit nil if name is not set, adds ‘<’ and ‘>’ around the name

Returns:



50
51
52
# File 'lib/ruby_units/definition.rb', line 50

def name
  "<#{@name}>" if defined?(@name) && @name
end

#name=(name_value) ⇒ String

set the name, strip off ‘<’ and ‘>’

Parameters:

Returns:



57
58
59
# File 'lib/ruby_units/definition.rb', line 57

def name=(name_value)
  @name = name_value.gsub(/[<>]/, '')
end

#prefix?Boolean

is this definition for a prefix?

Returns:

  • (Boolean)


81
82
83
# File 'lib/ruby_units/definition.rb', line 81

def prefix?
  kind == :prefix
end

#unity?Boolean

Is this definition the unity definition?

Returns:

  • (Boolean)


87
88
89
# File 'lib/ruby_units/definition.rb', line 87

def unity?
  prefix? && scalar == 1
end