Sha256: ef4aa9c58e51d6433824b66ec21610c4219600985451a97392b1fa0400079831

Contents?: true

Size: 793 Bytes

Versions: 2

Compression:

Stored size: 793 Bytes

Contents

# frozen_string_literal: true

require "refinements/shared/ios/reread"

module Refinements
  # Provides additional enhancements to the IO primitive.
  module IO
    refine ::IO.singleton_class do
      def void
        new(sysopen("/dev/null", "w+")).then do |io|
          return io unless block_given?

          yield io
          io.tap(&:close)
        end
      end
    end

    refine ::IO do
      import_methods Shared::IOs::Reread

      def redirect other
        return self unless block_given?

        backup = dup
        reopen other
        yield self
        reopen backup
      end

      # rubocop:todo Naming/BlockForwarding
      def squelch(&block) = self.class.void.then { |void| redirect(void, &block) }
      # rubocop:enable Naming/BlockForwarding
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
refinements-12.0.1 lib/refinements/io.rb
refinements-12.0.0 lib/refinements/io.rb