Sha256: 636fbed0efc8abdb49d8b50a3bc7350b2afdcc48fe583164a393a99719f241dc

Contents?: true

Size: 1.21 KB

Versions: 4

Compression:

Stored size: 1.21 KB

Contents

# Must be defined outside of Kernel for jruby, see http://jira.codehaus.org/browse/JRUBY-3609
Enumerator = Enumerable::Enumerator unless Object.const_defined? :Enumerator # Standard in ruby 1.9

class Enumerator
  # new with block, standard in Ruby 1.9
  unless (self.new{} rescue false)
      # A simple class which allows the construction of Enumerator from a block
    class Yielder
      def initialize(&block)
        @final_block = block
      end

      def yield(*arg)
        @final_block.yield(*arg)
      end

      def <<(*arg)
        @final_block.yield(*arg)
        self
      end
    end

    class GeneratorBP # Avoid conflict with 1.8's ::Generator class
      def initialize(&block)
        @main_block = block
      end

      def each(&block)
        @main_block.call(Yielder.new(&block))
      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(GeneratorBP.new(&block))
    end
    Backports.alias_method_chain self, :initialize, :optional_block
  end

  Backports.alias_method self, :with_object, :each_with_object
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
backports-3.0.3 lib/backports/1.9.1/enumerator.rb
backports-3.0.2 lib/backports/1.9.1/enumerator.rb
backports-3.0.1 lib/backports/1.9.1/enumerator.rb
backports-3.0.0 lib/backports/1.9.1/enumerator.rb