All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jruby.kernel19.enumerable.rb Maven / Gradle / Ivy

There is a newer version: 0.8.14
Show newest version
module Enumerable
  def slice_before(filter = (no_filter = true; nil), &block)
    if no_filter && !block
      raise ArgumentError.new("wrong number of arguments (0 for 1)")
    end

    if block
      if no_filter
        state = nil
      else
        initial_state = filter.dup
        state = initial_state
      end
    else
      state = nil
    end

    Enumerator.new do |yielder|
      ary = nil
      self.each do |elt|
        if block
          if no_filter
            state = block.call elt
          else
            state = block.call elt, initial_state
          end
        else
          state = (filter === elt)
        end

        if ary
          if state
            yielder.yield ary
            ary = [elt]
          else
            ary << elt
          end
        else
          ary = [elt]
        end
      end
      yielder.yield ary
    end
  end
end




© 2015 - 2025 Weber Informatics LLC | Privacy Policy