This is a continuation of Converting Celsius to Fahrenheit with Python, and TypeScript, and Go but in Ruby:


def c2f(c)
    c * 9.0 / 5 + 32;
end

def is_mirror(a, b)
    def massage(n)
        if n < 10
            "0#{n}"
        elsif n >= 100
            massage(n - 100)
        else
            n.to_s
        end
    end
    massage(a).reverse == massage(b)
end

def print_conv(c, f)
    puts "#{c}°C ~= #{f}°F"
end

(4...100).step(12).each do |c|
    f = c2f(c)
    if is_mirror(c, f.ceil)
        print_conv(c, f.ceil)
    elsif is_mirror(c, f.floor)
        print_conv(c, f.floor)
    else
        break
    end
end

Run it like this:


ruby conversion.rb

and the output becomes:

4°C ~= 40°F
16°C ~= 61°F
28°C ~= 82°F
40°C ~= 104°F
52°C ~= 125°F

Comments

Your email will never ever be published.

Related posts