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

Instance Attribute Summary

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, #dimension, #dimension_value, #dimensionless?, #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.


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

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


631
632
633
# File 'lib/phys/units/unit.rb', line 631

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)

653
654
655
# File 'lib/phys/units/unit.rb', line 653

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)

646
647
648
# File 'lib/phys/units/unit.rb', line 646

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

#operable?Boolean

Returns false

Returns:

  • (Boolean)

658
659
660
# File 'lib/phys/units/unit.rb', line 658

def operable?
  false
end