Ruby - Strings - Indexing

  • + 0 comments
    def serial_average(key = "001-12.43-56.78")
        serial = key[0,3]
        number1 = key[4, 5]
        number2 = key[10, 15]
        
        avg = (number1.to_f + number2.to_f) / 2
        avg = avg.round(2)
        key[4,11] = avg.to_s
        return key
    end