class Diffy::Diff

def diff

def diff
  @diff ||= begin
    @paths = case options[:source]
      when 'strings'
        [tempfile(string1), tempfile(string2)]
      when 'files'
        [string1, string2]
      end
    diff, _stderr, _process_status = Open3.capture3(diff_bin, *(diff_options + @paths))
    diff.force_encoding('ASCII-8BIT') if diff.respond_to?(:valid_encoding?) && !diff.valid_encoding?
    if diff =~ /\A\s*\Z/ && !options[:allow_empty_diff]
      diff = case options[:source]
      when 'strings' then string1
      when 'files' then File.read(string1)
      end.gsub(/^/, " ")
    end
    diff
  end
ensure
  # unlink the tempfiles explicitly now that the diff is generated
  if defined? @tempfiles # to avoid Ruby warnings about undefined ins var.
    Array(@tempfiles).each do |t|
      begin
        # check that the path is not nil and file still exists.
        # REE seems to be very agressive with when it magically removes
        # tempfiles
        t.unlink if t.path && File.exist?(t.path)
      rescue => e
        warn "#{e.class}: #{e}"
        warn e.backtrace.join("\n")
      end
    end
  end
end