Sha256: c34c33ef68ebf802da693077ec906d307ce240f8dfec1594b5e3162dbd2b8608

Contents?: true

Size: 699 Bytes

Versions: 5

Compression:

Stored size: 699 Bytes

Contents

# frozen_string_literal: true

module Refinements
  # Provides additional enhancements to the IO primitive.
  module IOs
    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
      def redirect other
        return self unless block_given?

        backup = dup
        reopen other
        yield self
        reopen backup
      end

      def reread(length = nil, buffer: nil) = tap(&:rewind).read(length, buffer)

      def squelch(&block) = self.class.void.then { |void| redirect(void, &block) }
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
refinements-8.5.2 lib/refinements/ios.rb
refinements-8.5.1 lib/refinements/ios.rb
refinements-8.5.0 lib/refinements/ios.rb
refinements-8.4.1 lib/refinements/ios.rb
refinements-8.3.0 lib/refinements/ios.rb