Sha256: 9317c996f94d7e235c90f7dee972c3ad036319bcb65368a8cd784eaa275271cc

Contents?: true

Size: 926 Bytes

Versions: 26

Compression:

Stored size: 926 Bytes

Contents

unless Enumerable.method_defined? :slice_before
  require 'backports/tools'
  require 'backports/1.9.1/enumerator/new'

  module Enumerable
    def slice_before(arg = Backports::Undefined, &block)
      if block_given?
        has_init = !(arg.equal? Backports::Undefined)
      else
        raise ArgumentError, "wrong number of arguments (0 for 1)" if arg.equal? Backports::Undefined
        block = Proc.new{|elem| arg === elem }
      end
      Enumerator.new do |yielder|
        init = arg.dup if has_init
        accumulator = nil
        each do |elem|
          start_new = has_init ? block.call(elem, init) : block.call(elem)
          if start_new
            yielder.yield accumulator if accumulator
            accumulator = [elem]
          else
            accumulator ||= []
            accumulator << elem
          end
        end
        yielder.yield accumulator if accumulator
      end
    end
  end
end

Version data entries

26 entries across 26 versions & 2 rubygems

Version Path
scout_realtime-1.0.5 lib/vendor/backports-3.3.5/lib/backports/1.9.2/enumerable/slice_before.rb
scout_realtime-1.0.4 lib/vendor/backports-3.3.5/lib/backports/1.9.2/enumerable/slice_before.rb
scout_realtime-1.0.3 lib/vendor/backports-3.3.5/lib/backports/1.9.2/enumerable/slice_before.rb
scout_realtime-1.0.3.pre lib/vendor/backports-3.3.5/lib/backports/1.9.2/enumerable/slice_before.rb
scout_realtime-1.0.2 lib/vendor/backports-3.3.5/lib/backports/1.9.2/enumerable/slice_before.rb
scout_realtime-1.0.1 lib/vendor/backports-3.3.5/lib/backports/1.9.2/enumerable/slice_before.rb
scout_realtime-1.0.0 lib/vendor/backports-3.3.5/lib/backports/1.9.2/enumerable/slice_before.rb
scout_realtime-0.5.5 lib/vendor/backports-3.3.5/lib/backports/1.9.2/enumerable/slice_before.rb
scout_realtime-0.5.5.pre lib/vendor/backports-3.3.5/lib/backports/1.9.2/enumerable/slice_before.rb
scout_realtime-0.5.4 lib/vendor/backports-3.3.5/lib/backports/1.9.2/enumerable/slice_before.rb
backports-3.6.0 lib/backports/1.9.2/enumerable/slice_before.rb
backports-3.5.0 lib/backports/1.9.2/enumerable/slice_before.rb
backports-3.4.1 lib/backports/1.9.2/enumerable/slice_before.rb
scout_realtime-0.5.3 lib/vendor/backports-3.3.5/lib/backports/1.9.2/enumerable/slice_before.rb
scout_realtime-0.5.2 lib/vendor/backports-3.3.5/lib/backports/1.9.2/enumerable/slice_before.rb
scout_realtime-0.5.1 lib/vendor/backports-3.3.5/lib/backports/1.9.2/enumerable/slice_before.rb
backports-3.4.0 lib/backports/1.9.2/enumerable/slice_before.rb
backports-3.3.5 lib/backports/1.9.2/enumerable/slice_before.rb
backports-3.3.4 lib/backports/1.9.2/enumerable/slice_before.rb
backports-3.3.3 lib/backports/1.9.2/enumerable/slice_before.rb