Ruby - Methods - Keyword Arguments

  • + 0 comments
    def convert_temp(temp, input_scale="celsius", output_scale="celsius"):
        scales = {
            "celsius": (1, 0),
            "kelvin": (1, 273.15),
            "fahrenheit": (1.8, 32)
        }
    
        factor_in, offset_in = scales.get(input_scale, (1, 0))
        factor_out, offset_out = scales.get(output_scale, (1, 0))
    
        temp_celsius = (temp - offset_in) / factor_in
    
        result = (temp_celsius * factor_out) + offset_out
    
        return result
    end