Replacing consecutive numbers with dash between first and last in a range

def replace_consecutive_with_dash(arr)
  result = []
  start_num = arr[0]

  arr.each_with_index do |num, index|
    if index > 0 && num != arr[index - 1] + 1
      result << (start_num == arr[index - 1] ? start_num.to_s : "#{start_num}-#{arr[index - 1]}")
      start_num = num
    end
  end

  result << (start_num == arr[-1] ? start_num.to_s : "#{start_num}-#{arr[-1]}")
  result
end

# Example usage:
input_array = [1, 2, 3, 5, 6, 8, 10, 11, 12]
output_array = replace_consecutive_with_dash(input_array)
puts output_array.join(', ')