Попытка перейти к базам и обратно в Ruby ... код, который я изложил, кажется довольно повторяющимся. Есть ли способ лучше?
module Converter
def self.convert(value, from, to)
case from
when :hex
case to
when :dec
# code to change hex to dec
when :oct
# code to change hex to oct
when :bin
# code to change hex to bin
when :ascii
# code to change hex to ascii
end
when :dec
case to
when :hex
# code to change dec to hex
when :oct
# code to change dec to oct
when :bin
# code to change dec to bin
when :ascii
# code to change dec to ascii
end
when :oct
case to
when :hex
# code to change oct to hex
when :dec
# code to change oct to dec
when :bin
# code to change oct to bin
when :ascii
# code to change oct to ascii
end
when :bin
case to
when :hex
# code to change bin to hex
when :dec
# code to change bin to dec
when :oct
# code to change bin to oct
when :ascii
# code to change bin to ascii
end
when :ascii
case to
when :hex
# code to change ascii to hex
when :dec
# code to change ascii to dec
when :oct
# code to change ascii to oct
when :bin
# code to change ascii to bin
end
end
end
end