Ruby - Methods - Keyword Arguments

Sort by

recency

|

102 Discussions

|

  • + 1 comment

    I'm with a problem with my code, because it works when I run it on VScode, but when I try on HackerRank, it don't works

    def convert_temp(temperature,input_scale:,output_scale:'celsius') if input_scale=='celsius' if output_scale=='kelvin'

      temperature+=273.15
    
    elsif output_scale=='fahrenheit'
    
      temperature=((temperature*9/5)+32).to_f
    
    end
    

    elsif input_scale=='kelvin'

    if output_scale=='celsius'
    
      temperature-=273.15
    
    elsif output_scale=='fahrenheit'
    
      temperature=(((temperature-273.15)*9/5)+32).to_f
    
    end
    

    elsif input_scale=='fahrenheit'

    if output_scale=='celsius'
    
      temperature = ((temperature-32)*5/9).to_f
    
    elsif output_scale=='kelvin'
    
      temperature =(((temperature-32)*5/9)+273.15).to_f
    end
    

    end end

    puts convert_temp(0, input_scale: 'celsius', output_scale: 'kelvin')

  • + 0 comments

    Using Ruby Symbols and Lambdas to get a readable table of conversions.

    def convert_temp(temperature, input_scale:, output_scale: :celsius)    
        {
            [:celsius, :fahrenheit] => ->(t) { (t * 9.0/5.0) + 32.0 },
            [:celsius, :kelvin] => ->(t) { t + 273.15 },
            [:fahrenheit, :celsius] => ->(t) { (t - 32.0) * 5.0/9.0 },
            [:fahrenheit, :kelvin] => ->(t) { ((t - 32.0) * 5.0/9.0) + 273.15 },
            [:kelvin, :celsius] => ->(t) { t - 273.15 },
            [:kelvin, :fahrenheit] => ->(t) { (t - 273.15) * 9.0/5.0 + 32.0 },
        }[[input_scale.to_sym, output_scale.to_sym]].call(temperature)
    end
    
  • + 0 comments
    def convert_temp(temp, input_scale: String, output_scale: 'celsius')
      conversion = [
        input_scale.strip.downcase.to_sym,
        output_scale.strip.downcase.to_sym
      ]
      k_to_c = lambda { |k| k - 273.15 }
      c_to_k = lambda { |c| c + 273.15 }
      c_to_f = lambda { |c| (c * (9.0/5.0)) + 32.0 }
      f_to_c = lambda { |f| (f - 32.0) * (5.0/9.0) }
    
      case conversion
        when [:kelvin, :kelvin]
          temp
        when [:celsius, :celsius]
          temp
        when [:fahrenheit, :fahrenheit]
          temp
        when [:kelvin, :celsius]
          k_to_c.call(temp)
        when [:celsius, :kelvin]
          c_to_k.call(temp)
        when [:kelvin, :fahrenheit]
          c_to_f.call(k_to_c.call(temp))
        when [:fahrenheit, :kelvin]
          c_to_k.call(f_to_c.call(temp))
        when [:celsius, :fahrenheit]
          c_to_f.call(temp)
        when [:fahrenheit, :celsius]
          f_to_c.call(temp)
        else
          raise StandardError, "Invalid Units: #{input_scale} or #{output_scale}"
      end
    end
    
  • + 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
    
  • + 0 comments
    def convert_temp(temp, **option)
         if option[:input_scale] == "kelvin" 
            temp = temp - 273.15
        elsif option[:input_scale] == "fahrenheit" 
            temp = ((temp - 32) / 1.8)
        end
         
        if option[:output_scale] == "kelvin"
           temp =  temp + 273.15
        elsif option[:output_scale] == "fahrenheit"
          temp = 1.8 * temp +32
        end
        temp
    end