Class: Phys::OffsetUnit

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

Overview

OffsetUnit is a class to represent units with offset value. Allows Fahrenheit/Celsius temperature. Unit operations are not allowed.

Examples:

require 'phys/units'

Phys::Quantity[20,:tempC].want(:tempF)                #=> Phys::Quantity[68,'tempF']
Phys::Quantity[3,:tempC] + Phys::Quantity[10,:degF]   #=> Phys::Quantity[(77/9),"tempC"]
Phys::Quantity[3,:tempC] * Phys::Quantity[3,:m]       #=> UnitError
Phys::Quantity[3,:tempC] ** 2                         #=> UnitError

# Next examples currently work, but I suggest the use of degC and degF.
Phys::Quantity[3,:tempC] + Phys::Quantity[10,:tempF]  #=> Phys::Quantity[(77/9),"tempC"]
Phys::Quantity[3,:tempC] * 2                          #=> Phys::Quantity[6,"tempC"]

Constant Summary

Constants inherited from Unit

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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Unit

#*, #**, #+, #+@, #-, #-@, #/, #==, #===, #base_unit, cast, #coerce, #conversion_factor, #convert, #convert_scale, #dimension, #dimension_value, #dimensionless?, #expr, #factor, find_unit, import_units, #inspect, #inverse, parse, prefix_regex, #scalar?, #to_numeric, #to_s, #unit_string

Constructor Details

#initialize(arg, name = nil, offset = nil) ⇒ OffsetUnit

Returns a new instance of OffsetUnit.



637
638
639
640
641
642
643
# File 'lib/phys/units/unit.rb', line 637

def initialize(arg,name=nil,offset=nil)
  if offset.nil?
    raise ArgumentError,"offset is not supplied"
  end
  super(arg,name)
  @offset = offset
end

Class Method Details

.define(name, unit, offset = nil) ⇒ Object



633
634
635
# File 'lib/phys/units/unit.rb', line 633

def self.define(name,unit,offset=nil)
  LIST[name] = self.new(unit,name,offset)
end

Instance Method Details

#convert_value_from_base_unit(value) ⇒ Numeric

Convert from a value in base unit to a value in this unit.

Parameters:

  • value (Numeric)

Returns:

  • (Numeric)


655
656
657
# File 'lib/phys/units/unit.rb', line 655

def convert_value_from_base_unit(value)
  (value - @offset) / conversion_factor
end

#convert_value_to_base_unit(value) ⇒ Numeric

Convert from a value in this unit to a value in base unit.

Parameters:

  • value (Numeric)

Returns:

  • (Numeric)


648
649
650
# File 'lib/phys/units/unit.rb', line 648

def convert_value_to_base_unit(value)
  value * conversion_factor + @offset
end

#operable?Boolean

Returns false

Returns:

  • (Boolean)


660
661
662
# File 'lib/phys/units/unit.rb', line 660

def operable?
  false
end