class Variable < Struct.new(:value) def to_s "#{value} #{self.class.to_s.downcase}s" end def inspect to_s end def and(other) current = resistance = voltage = nil [self, other].each do |element| case element when Ampere current = element.value when Ohm resistance = element.value when Volt voltage = element.value end end if current.nil? Ampere.new(voltage / resistance) elsif resistance.nil? Ohm.new(voltage / current) elsif voltage.nil? Volt.new(current * resistance) end end end class Ampere < Variable end class Ohm < Variable end class Volt < Variable end