class Convert

Public Class Methods

convert_absolute(args) click to toggle source

The [convert_absolute] method converts a absolute temperature to another unit @param args [Hash]

* :input [Double] the temperature value.
* :from_unit [String] the unit of :input
* :to_unit [String] the unit of the return value

@return [Double] the value in :to_unit @example

Convert.convert_relative(input: 20, from_unit: 'celcius', to_unit: 'kelvin')
#returns 293.15
# 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) click to toggle source

The [convert_relative] method converts a relative temperature to another unit @param args [Hash]

* :input [Double] the temperature value.
* :from_unit [String] the unit of :input
* :to_unit [String] the unit of the return value

@return [Double] the value in :to_unit @example

Convert.convert_relative(input: 20, from_unit: 'celcius', to_unit: 'kelvin')
#returns 20
# 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