<commit_before>require 'open4'

require_relative 'capture_output_streams/system'
require_relative 'capture_output_streams/backticks'

module CaptureOutputStreams

  Kernel.module_eval do

    def capture_output_streams(&block)
      original_stdout = $stdout
      original_stderr = $stderr
      $stdout = fake_stdout = StringIO.new
      $stderr = fake_stderr = StringIO.new
      begin
        yield
      ensure
        $stdout = original_stdout
        $stderr = original_stderr
      end
      {
        :stdout => fake_stdout.string,
        :stderr => fake_stderr.string,
      }
    end

  end

end
<commit_msg>add dynamic kernel methods faking and restore<commit_after>require 'open4'

module CaptureOutputStreams

  Kernel.module_eval do

    def capture_output_streams(&block)
      path = File.dirname(__FILE__)

      # save original kernel methods
      Kernel.module_eval do
        alias_method :orig_system,    :system
        alias_method :orig_backticks, :`
      end

      # save original output streams
      original_stdout = $stdout
      original_stderr = $stderr

      # override original kernel methods
      load File.join(path, 'capture_output_streams/system.rb')
      load File.join(path, 'capture_output_streams/backticks.rb')

      # override original output streams
      $stdout = fake_stdout = StringIO.new
      $stderr = fake_stderr = StringIO.new

      begin
        yield

      ensure
        # restore original kernel methods
        Kernel.module_eval do
          alias_method :system, :orig_system
          alias_method :`,      :orig_backticks
        end

        # restore original output streams
        $stdout = original_stdout
        $stderr = original_stderr

      end

      {
        :stdout => fake_stdout.string,
        :stderr => fake_stderr.string,
      }

    end

  end

end

{'fraction_non_alphanumeric': 0.06612466124661247, 'fraction_numerical': 0.001084010840108401, 'mean_word_length': 2.4059040590405902, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '6788935', 'n_tokens_mistral': 526, 'n_tokens_neox': 507, 'n_words': 124}