class Array
  def extract_options!
    last.is_a?(::Hash) ? pop : {}
  end
end

class Hash
  def reverse_merge(other_hash)
    other_hash.merge(self)
  end

  def reverse_merge!(other_hash)
    replace(reverse_merge(other_hash))
  end
end

def number_with_delimiter(number, *args)
  # extract hash to support new API
  options = args.extract_options!
  # check array to support current API
  if args.size > 0
    options[:delimiter] = args[0] || ","
    options[:separator] = args[1] || "."
  end
  options.reverse_merge!(:delimiter => ",", :separator => ".")

  begin
    parts = number.to_s.split('.')
    parts[0].gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{options[:delimiter]}")
    parts.join options[:separator]
  rescue
    number
  end
end

puts number_with_delimiter(1999.99)
puts number_with_delimiter(1999.99, ".")
puts number_with_delimiter(1999.99, :separator => ",", :delimiter => ".")
puts number_with_delimiter(1999.99, :separator => ",")