Sha256: 6655924fe9774e6b3a138bacdc0ad14e4116b8b653da7968ea05c57a948cf41a
Contents?: true
Size: 1.05 KB
Versions: 2
Compression:
Stored size: 1.05 KB
Contents
class Base attr_accessor :value, :unit def initialize(value, unit, valid_units) raise TypeError, 'no implicit conversion of String into Integer' unless value.is_a? Numeric raise TypeError, 'Invalid Unit Type' unless valid_units.include?(unit.to_s.downcase) @value = value @unit = unit.downcase end def ==(other) a_to_b = one_unit_a_to_b(unit, other.unit) * value b_to_a = one_unit_a_to_b(other.unit, unit) * other.value a_to_b == other.value || b_to_a == value end def ===(other) value == other.value && unit == other.unit end def +(other) b_to_a = other.to(unit) self.class.new(value + b_to_a.value, unit) end def -(other) b_to_a = other.to(unit) self.class.new(value - b_to_a.value, unit) end def inspect "#{@value}#{@unit}" end protected def one_unit_a_to_b(unit_a, unit_b) conversion_rate = @conversion_rate_for_one_unit[unit_a][unit_b] format('%<value>f', value: conversion_rate).to_f end def convert_to(c_unit) one_unit_a_to_b(unit, c_unit) * value end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
convert_unit-0.2.0 | lib/convert_unit/base.rb |
convert_unit-0.1.0 | lib/convert_unit/base.rb |