Sha256: 39d5678da6d38c0853b6bb65c7bb1ac306d6861f1d61f36ad610ca4cc369585a

Contents?: true

Size: 670 Bytes

Versions: 9

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

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

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
refinements-8.0.1 lib/refinements/ios.rb
refinements-8.0.0 lib/refinements/ios.rb
refinements-7.18.0 lib/refinements/ios.rb
refinements-7.17.0 lib/refinements/ios.rb
refinements-7.16.0 lib/refinements/ios.rb
refinements-7.15.1 lib/refinements/ios.rb
refinements-7.15.0 lib/refinements/ios.rb
refinements-7.14.0 lib/refinements/ios.rb
refinements-7.13.0 lib/refinements/ios.rb