Class: Tempura::Temperature

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/tempura/temperature.rb

Overview

Parent class of all Temperature objects in Tempura, inherited by Tempura::Celsius, etc.

Direct Known Subclasses

Celsius, Delisle, Fahrenheit, Kelvin, Newton, Rankine, Réaumur, Rømer

Instance Method Summary collapse

Constructor Details

#initialize(temp) ⇒ Temperature

Returns a new instance of Temperature.



7
8
9
10
11
12
13
# File 'lib/tempura/temperature.rb', line 7

def initialize(temp)
  if temp.is_a?(Tempura::Temperature)
    @common = temp.send(:common)
  else
    @common = to_common(temp)
  end
end

Instance Method Details

#*(temp) ⇒ Object

Multiplies by Numeric.

Parameters:



53
54
55
56
57
58
# File 'lib/tempura/temperature.rb', line 53

def *(temp)
  if temp.is_a?(Tempura::Temperature)
    raise ArgumentError, "cannot multiply by a Tempura::Temperature, only a Numeric"
  end
  self.class.new(in_native * temp)
end

#+(temp) ⇒ Object

Adds a Temperature of any scale, or a numeric representation of the same scale as the receiver.

Parameters:



36
37
38
# File 'lib/tempura/temperature.rb', line 36

def +(temp)
  self.class.new(in_native + numericalize(temp))
end

#-(temp) ⇒ Object

Subtracts a Temperature of any scale, or a numeric representation of the same scale as the receiver.

Parameters:



29
30
31
# File 'lib/tempura/temperature.rb', line 29

def -(temp)
  self.class.new(in_native - numericalize(temp))
end

#/(temp) ⇒ Object

Divides by Numeric.

Parameters:



43
44
45
46
47
48
# File 'lib/tempura/temperature.rb', line 43

def /(temp)
  if temp.is_a?(Tempura::Temperature)
    raise ArgumentError, "cannot divide by a Tempura::Temperature, only a Numeric"
  end
  self.class.new(in_native.div(temp))
end

#<=>(temp) ⇒ Object

Compares against a Temperature of any scale, or a numeric representation of the same scale as the receiver.

Parameters:



18
19
20
21
22
23
24
# File 'lib/tempura/temperature.rb', line 18

def <=>(temp)
  if temp.is_a?(Tempura::Temperature)
    @common <=> temp.send(:common)
  else
    @common <=> to_common(temp)
  end
end

#toObject

Returns Conversion for the receiver, which recieves the name of the scale to convert to. E.g.,

c = Tempura::Celsius.new(100)
c.to.fahrenheit #=> <Tempura::Fahrenheit ...


65
66
67
# File 'lib/tempura/temperature.rb', line 65

def to
  Tempura::Conversion.new(self)
end

#to_dObject

BigDecimal representation of receiver



80
81
82
# File 'lib/tempura/temperature.rb', line 80

def to_d
  in_native
end

#to_fObject

Float representation of receiver



70
71
72
# File 'lib/tempura/temperature.rb', line 70

def to_f
  in_native.to_f
end

#to_iObject

Integer representation of receiver



75
76
77
# File 'lib/tempura/temperature.rb', line 75

def to_i
  in_native.to_i
end