<commit_before>module MetricFu
  module Io
    # Writes the output to a file or io stream.
    # @param output [String, #to_s] the content to write.
    # @param path_or_io [String, #to_s, IO, nil] a file path or an
    # io stream that responds to write. Can be nil. If nil,
    # output is not written.
    # @return [nil]
    def write_output(output, path_or_io)
      io_for(path_or_io) do |io|
        io.write(output)
      end
    end

    # Yields an io object for writing output.
    # @example
    #   io_for('path/to/file') do |io|
    #     io.write(output)
    #   end
    #
    #   io_for(STDOUT) do |io|
    #     io.write(output)
    #   end
    #
    #   stream = StringIO.new
    #   io_for(stream) do |io|
    #     io.write(output)
    #   end
    #
    # @param path_or_io [String, IO, nil] a file path or an
    # io stream that responds to write. Can be nil. If nil,
    # block is ignored.
    #
    # @yield [IO] an open stream for writing.
    #
    # @note Given a path to a file, an open file will
    # be yielded and closed after the block completes.
    # Given an existing io stream, the stream will not
    # be automatically closed. Cleanup, if necessary, is
    # the responsibility of the caller.
    def io_for(path_or_io)
      return nil if path_or_io.nil?

      if path_or_io.respond_to?(:write)
        # We have an existing open stream...<commit_msg>Fix yardoc indentation.<commit_after>module MetricFu
  module Io
    # Writes the output to a file or io stream.
    # @param output [String, #to_s] the content to write.
    # @param path_or_io [String, #to_s, IO, nil] a file path or an
    #   io stream that responds to write. Can be nil. If nil,
    #   output is not written.
    # @return [nil]
    def write_output(output, path_or_io)
      io_for(path_or_io) do |io|
        io.write(output)
      end
    end

    # Yields an io object for writing output.
    # @example
    #   io_for('path/to/file') do |io|
    #     io.write(output)
    #   end
    #
    #   io_for(STDOUT) do |io|
    #     io.write(output)
    #   end
    #
    #   stream = StringIO.new
    #   io_for(stream) do |io|
    #     io.write(output)
    #   end
    #
    # @param path_or_io [String, IO, nil] a file path or an
    #   io stream that responds to write. Can be nil. If nil,
    #   block is ignored.
    #
    # @yield [IO] an open stream for writing.
    #
    # @note Given a path to a file, an open file will
    #   be yielded and closed after the block completes.
    #   Given an existing io stream, the stream will not
    #   be automatically closed. Cleanup, if necessary, is
    #   the responsibility of the caller.
    def io_for(path_or_io)
      return nil if path_or_io.nil?

      if path_or_io.respond_to?(:write)
        # We have an existing open stream...
{'fraction_non_alphanumeric': 0.09210526315789473, 'fraction_numerical': 0.0, 'mean_word_length': 2.153587443946188, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1832622', 'n_tokens_mistral': 961, 'n_tokens_neox': 942, 'n_words': 337}