Class: RubyUnits::Unit::Definition
- Defined in:
- lib/ruby_units/definition.rb
Overview
Handle the definition of units
Instance Attribute Summary collapse
-
#aliases ⇒ Array
alias array must contain the name of the unit and entries must be unique.
- #denominator ⇒ Array
- #display_name ⇒ String
- #kind ⇒ Symbol
- #numerator ⇒ Array
- #scalar ⇒ Numeric
Instance Method Summary collapse
-
#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.
-
#definition=(unit) ⇒ Unit::Definition
define a unit in terms of another unit.
-
#initialize(_name, _definition = []) {|_self| ... } ⇒ Definition
constructor
A new instance of Definition.
-
#name ⇒ String?
name of the unit nil if name is not set, adds ‘<’ and ‘>’ around the name.
-
#name=(_name) ⇒ String
set the name, strip off ‘<’ and ‘>’.
-
#prefix? ⇒ Boolean
is this definition for a prefix?.
-
#unity? ⇒ Boolean
Is this definition the unity definition?.
Constructor Details
#initialize(_name, _definition = []) {|_self| ... } ⇒ Definition
Returns a new instance of Definition.
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/ruby_units/definition.rb', line 33 def initialize(_name, _definition = [], &block) 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
#aliases ⇒ Array
alias array must contain the name of the unit and entries must be unique
61 62 63 |
# File 'lib/ruby_units/definition.rb', line 61 def aliases [[@aliases], @name].flatten.compact.uniq end |
#denominator ⇒ Array
19 20 21 |
# File 'lib/ruby_units/definition.rb', line 19 def denominator @denominator end |
#display_name ⇒ String
22 23 24 |
# File 'lib/ruby_units/definition.rb', line 22 def display_name @display_name end |
#kind ⇒ Symbol
10 11 12 |
# File 'lib/ruby_units/definition.rb', line 10 def kind @kind end |
#numerator ⇒ Array
16 17 18 |
# File 'lib/ruby_units/definition.rb', line 16 def numerator @numerator 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.
92 93 94 95 96 97 98 |
# File 'lib/ruby_units/definition.rb', line 92 def base? (self.denominator == RubyUnits::Unit::UNITY_ARRAY) && (self.numerator != RubyUnits::Unit::UNITY_ARRAY) && (self.numerator.size == 1) && (self.scalar == 1) && (self.numerator.first == self.name) end |
#definition=(unit) ⇒ Unit::Definition
define a unit in terms of another unit
68 69 70 71 72 73 74 75 |
# File 'lib/ruby_units/definition.rb', line 68 def definition=(unit) _base = unit.to_base @scalar = _base.scalar @kind = _base.kind @numerator = _base.numerator @denominator = _base.denominator self end |
#name ⇒ String?
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
48 49 50 |
# File 'lib/ruby_units/definition.rb', line 48 def name "<#{@name}>" if (defined?(@name) && @name) end |
#name=(_name) ⇒ String
set the name, strip off ‘<’ and ‘>’
55 56 57 |
# File 'lib/ruby_units/definition.rb', line 55 def name=(_name) @name = _name.gsub(/[<>]/,'') end |
#prefix? ⇒ Boolean
is this definition for a prefix?
79 80 81 |
# File 'lib/ruby_units/definition.rb', line 79 def prefix? self.kind == :prefix end |
#unity? ⇒ Boolean
Is this definition the unity definition?
85 86 87 |
# File 'lib/ruby_units/definition.rb', line 85 def unity? self.prefix? && self.scalar == 1 end |