Sha256: 679160e9d7a47c439daf871f316d1768e62372918805d7814e11107d9983e1cf

Contents?: true

Size: 670 Bytes

Versions: 2

Compression:

Stored size: 670 Bytes

Contents

# frozen_string_literal: true

module Refinements
  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 squelch &block
        self.class.void.then { |void| redirect(void, &block) }
      end

      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
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
refinements-7.12.0 lib/refinements/ios.rb
refinements-7.11.0 lib/refinements/ios.rb