Sha256: 230e696f848bc252ceb7efa728d5888be5bc52bf802552af24c1f9c56c1b52a1

Contents?: true

Size: 1.36 KB

Versions: 1

Compression:

Stored size: 1.36 KB

Contents

require 'English'

module Polyfill
  module V2_4
    module IO
      module Class
        module Foreach
          module Method
            def foreach(name, *args)
              hash, others = args.partition { |arg| arg.is_a?(::Hash) }
              chomps = hash[0] && hash[0][:chomp]

              unless block_given?
                return super(name, *others) unless chomps

                separator = others.find do |other|
                  other.respond_to?(:to_str)
                end || $INPUT_RECORD_SEPARATOR

                return ::Enumerator.new do |yielder|
                  super(name, *others) do |line|
                    yielder.yield(line.chomp(separator))
                  end
                end
              end

              block =
                if chomps
                  separator = others.find do |other|
                    other.respond_to?(:to_str)
                  end || $INPUT_RECORD_SEPARATOR

                  proc do |line|
                    yield(line.chomp(separator))
                  end
                else
                  ::Proc.new
                end

              super(name, *others, &block)
            end
          end

          refine ::IO.singleton_class do
            include Method
          end

          def self.included(base)
            base.include Method
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
polyfill-0.6.0 lib/polyfill/v2_4/io/class/foreach.rb