Class: Convert

Inherits:
Object
  • Object
show all
Defined in:
lib/convert.rb

Class Method Summary collapse

Class Method Details

.convert_absolute(args) ⇒ Double

The [convert_absolute] method converts a absolute temperature to another unit

Examples:

Convert.convert_relative(input: 20, from_unit: 'celcius', to_unit: 'kelvin')
#returns 293.15

Parameters:

  • args (Hash)
    • :input [Double] the temperature value.

    • :from_unit [String] the unit of :input

    • :to_unit [String] the unit of the return value

Returns:

  • (Double)

    the value in :to_unit



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/convert.rb', line 11

def self.convert_absolute(args)
  input = args.fetch(:input)
  from_unit = args.fetch(:from_unit)
  to_unit = args.fetch(:to_unit)
  return input if to_unit == from_unit
  if to_unit == 'celsius'
    return ((input - 32) * (5.0 / 9.0)) if from_unit == 'fahrenheit'
    return input - 273.15 if from_unit == 'kelvin'
  elsif to_unit == 'fahrenheit'
    return (input * (9.0 / 5.0)) + 32 if from_unit == 'celsius'
    return (input * (9.0 / 5.0)) - 459.67 if from_unit == 'kelvin'
  elsif to_unit == 'kelvin'
    return input + 273.15 if from_unit == 'celsius'
    return (input + 459.67) * (5.0 / 9.0) if from_unit == 'fahrenheit'
  end
end

.convert_relative(args) ⇒ Double

The [convert_relative] method converts a relative temperature to another unit

Examples:

Convert.convert_relative(input: 20, from_unit: 'celcius', to_unit: 'kelvin')
#returns 20

Parameters:

  • args (Hash)
    • :input [Double] the temperature value.

    • :from_unit [String] the unit of :input

    • :to_unit [String] the unit of the return value

Returns:

  • (Double)

    the value in :to_unit



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/convert.rb', line 37

def self.convert_relative(args)
  input = args.fetch(:input)
  from_unit = args.fetch(:from_unit)
  to_unit = args.fetch(:to_unit)
  if to_unit == 'celsius'
    return input / 1.8 if from_unit == 'fahrenheit'
  elsif to_unit == 'fahrenheit'
    return input * 1.8 if from_unit == 'celsius'
    return input * 1.8 if from_unit == 'kelvin'
  elsif to_unit == 'kelvin'
    return input / 1.8 if from_unit == 'fahrenheit'
  end
  input
end