Sha256: 3d044eb82fd07ac67d4a3c6ecdabd5eb2971acd39205b4c81ccd5590e6403467

Contents?: true

Size: 1.07 KB

Versions: 10

Compression:

Stored size: 1.07 KB

Contents

class Enumerator
  alias_method :with_object, :each_with_object unless method_defined? :with_object
   
  def next
    require 'generator'
    @generator ||= Generator.new(self)
    raise StopIteration unless @generator.next?
    @generator.next
  end unless method_defined? :next
  
  def rewind
    require 'generator'
    @generator ||= Generator.new(self)
    @generator.rewind
    self
  end unless method_defined? :rewind
  
  # new with block, standard in Ruby 1.9
  unless (self.new{} rescue false)
    class Yielder
      def initialize(&block)
        @main_block = block
      end
  
      def each(&block)
        @final_block = block
        @main_block.call(self)
      end
  
      def yield(*arg)
        @final_block.yield(*arg)
      end
    end
    
    def initialize_with_optional_block(*arg, &block)
      return initialize_without_optional_block(*arg, &nil) unless arg.empty?  # Ruby 1.9 apparently ignores the block if any argument is present
      initialize_without_optional_block(Yielder.new(&block))
    end
    alias_method_chain :initialize, :optional_block
  end
end

Version data entries

10 entries across 10 versions & 2 rubygems

Version Path
marcandre-backports-1.6.3 lib/backports/enumerator.rb
marcandre-backports-1.6.4 lib/backports/enumerator.rb
marcandre-backports-1.6.6 lib/backports/enumerator.rb
marcandre-backports-1.6.7 lib/backports/enumerator.rb
marcandre-backports-1.6.8 lib/backports/enumerator.rb
backports-1.6.7 lib/backports/enumerator.rb
backports-1.6.4 lib/backports/enumerator.rb
backports-1.6.3 lib/backports/enumerator.rb
backports-1.6.8 lib/backports/enumerator.rb
backports-1.6.6 lib/backports/enumerator.rb