Class: Phys::BaseUnit

Inherits:
Unit
  • Object
show all
Defined in:
lib/phys/units/unit.rb

Overview

BaseUnit is a class to represent units defined by “!” in the data form of GNU units. It includes SI units and dimensionless units such as radian.

Constant Summary

Constants inherited from Unit

Unit::LIST, Unit::PREFIX, Unit::VERSION

Instance Attribute Summary collapse

Attributes inherited from Unit

#expr

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Unit

#*, #**, #+, #[email protected], #-, #[email protected], #/, #==, #===, #base_unit, cast, #coerce, #conversion_factor, #convert, #convert_scale, #convert_value_from_base_unit, #convert_value_to_base_unit, #dimension, #factor, find_unit, import_units, #inspect, #inverse, #operable?, parse, prefix_regex, #scalar?, #to_numeric, #to_s, #unit_string

Constructor Details

#initialize(expr, name, dimval = nil) ⇒ BaseUnit

Returns a new instance of BaseUnit.


572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
# File 'lib/phys/units/unit.rb', line 572

def initialize(expr,name,dimval=nil)
  case name
  when String
    @name = name.strip
    @factor = 1
    @dim = {@name=>1}
    @dim.default = 0
    @expr = expr
    if String===expr && /!dimensionless/ =~ expr
      @dimensionless = true
    end
    @dimension_value = dimval || 1
  else
    raise ArgumentError,"Second argument must be string: #{name}"
  end
end

Instance Attribute Details

#dimension_valueNumeric (readonly)

Dimension value. Returns PI number for pi dimension, otherwise returns one.

Examples:

Phys::Unit["pi"].dimension_value #=> 3.141592653589793

Returns:

  • (Numeric)

611
612
613
# File 'lib/phys/units/unit.rb', line 611

def dimension_value
  @dimension_value
end

Class Method Details

.define(name, expr, dimval = nil) ⇒ Object


565
566
567
568
569
570
# File 'lib/phys/units/unit.rb', line 565

def self.define(name,expr,dimval=nil)
  if LIST[name]
    warn "unit definition is overwritten: #{name}" if debug
  end
  LIST[name] = self.new(expr,name,dimval)
end

Instance Method Details

#dimensionless?Boolean

Returns:

  • (Boolean)

593
594
595
# File 'lib/phys/units/unit.rb', line 593

def dimensionless?
  @dimensionless
end