class RubyUnits::Unit::Definition

Handle the definition of units

Attributes

aliases[W]

@return [Array]

denominator[RW]

@return [Array]

display_name[RW]

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.

@return [String]

kind[RW]

@return [Symbol]

numerator[RW]

@return [Array]

scalar[RW]

@return [Numeric]

Public Class Methods

new(name, definition = []) { |self| ... } click to toggle source

@example Raw definition from a hash

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

@example Block form

Unit::Definition.new("rack-unit") do |unit|
  unit.aliases = %w{U rack-U}
  unit.definition = RubyUnits::Unit.new("7/4 inches")
end
# File lib/ruby_units/definition.rb, line 37
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

Public Instance Methods

aliases() click to toggle source

alias array must contain the name of the unit and entries must be unique @return [Array]

# File lib/ruby_units/definition.rb, line 65
def aliases
  [[@aliases], @name, @display_name].flatten.compact.uniq
end
base?() click to toggle source

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

# File lib/ruby_units/definition.rb, line 95
def base?
  (denominator     == RubyUnits::Unit::UNITY_ARRAY) &&
    (numerator       != RubyUnits::Unit::UNITY_ARRAY) &&
    (numerator.size  == 1) &&
    (scalar          == 1) &&
    (numerator.first == self.name)
end
definition=(unit) click to toggle source

define a unit in terms of another unit @param [Unit] unit @return [Unit::Definition]

# File lib/ruby_units/definition.rb, line 72
def definition=(unit)
  base         = unit.to_base
  @scalar      = base.scalar
  @kind        = base.kind
  @numerator   = base.numerator
  @denominator = base.denominator
end
name() click to toggle source

name of the unit nil if name is not set, adds ‘<’ and ‘>’ around the name @return [String, nil] @todo refactor Unit and Unit::Definition so we don’t need to wrap units with angle brackets

# File lib/ruby_units/definition.rb, line 52
def name
  "<#{@name}>" if defined?(@name) && @name
end
name=(name_value) click to toggle source

set the name, strip off ‘<’ and ‘>’ @param name_value [String] @return [String]

# File lib/ruby_units/definition.rb, line 59
def name=(name_value)
  @name = name_value.gsub(/[<>]/, "")
end
prefix?() click to toggle source

is this definition for a prefix? @return [Boolean]

# File lib/ruby_units/definition.rb, line 82
def prefix?
  kind == :prefix
end
unity?() click to toggle source

Is this definition the unity definition? @return [Boolean]

# File lib/ruby_units/definition.rb, line 88
def unity?
  prefix? && scalar == 1
end