Class: RubyUnits::Unit::Definition
- Inherits:
-
Object
- Object
- 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
Unit name to be used when generating output.
- #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_value) ⇒ 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.
37 38 39 40 41 42 43 44 45 46 |
# 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 |
Instance Attribute Details
#aliases ⇒ Array
alias array must contain the name of the unit and entries must be unique
65 66 67 |
# File 'lib/ruby_units/definition.rb', line 65 def aliases [[@aliases], @name, @display_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
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.
26 27 28 |
# File 'lib/ruby_units/definition.rb', line 26 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.
95 96 97 98 99 100 101 |
# 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) ⇒ Unit::Definition
define a unit in terms of another unit
72 73 74 75 76 77 78 |
# 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 ⇒ 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
52 53 54 |
# File 'lib/ruby_units/definition.rb', line 52 def name "<#{@name}>" if defined?(@name) && @name end |
#name=(name_value) ⇒ String
set the name, strip off ‘<’ and ‘>’
59 60 61 |
# File 'lib/ruby_units/definition.rb', line 59 def name=(name_value) @name = name_value.gsub(/[<>]/, "") end |
#prefix? ⇒ Boolean
is this definition for a prefix?
82 83 84 |
# File 'lib/ruby_units/definition.rb', line 82 def prefix? kind == :prefix end |
#unity? ⇒ Boolean
Is this definition the unity definition?
88 89 90 |
# File 'lib/ruby_units/definition.rb', line 88 def unity? prefix? && scalar == 1 end |