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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Unit

#*, #**, #+, #+@, #-, #-@, #/, #==, #===, #base_unit, cast, #coerce, #conversion_factor, #convert, #convert_scale, #convert_value_from_base_unit, #convert_value_to_base_unit, #dimension, #expr, #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.



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

def initialize(expr,name,dimval=nil)
  case name
  when String
    @name = name.strip
    @factor = 1
    @dim = {@name=>1}
    @dim.default = 0
    @expr = nil
    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)


613
614
615
# File 'lib/phys/units/unit.rb', line 613

def dimension_value
  @dimension_value
end

Class Method Details

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



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

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)


595
596
597
# File 'lib/phys/units/unit.rb', line 595

def dimensionless?
  @dimensionless
end