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.

Previous:
Converting Celsius to Fahrenheit with Go July 17, 2024 Go
Next:
Converting Celsius to Fahrenheit with Crystal July 19, 2024 Ruby
Related by category:
Converting Celsius to Fahrenheit round-up July 22, 2024 Ruby
Related by keyword:
Comparing Deno vs Node vs Bun August 5, 2024 JavaScript, Bun
Converting Celsius to Fahrenheit round-up July 22, 2024 Python, Go, Node, JavaScript, Bun, Ruby, Rust
Converting Celsius to Fahrenheit with Python July 12, 2024 Python
Converting Celsius to Fahrenheit with Rust July 20, 2024 Rust