Sha256: 583781aea1beef0dcd93b893ac724c0333d1742795ca1510298f7d4203e8d98c
Contents?: true
Size: 1.61 KB
Versions: 6
Compression:
Stored size: 1.61 KB
Contents
module Enumerable # Standard in Ruby 1.9.2 See official documentation[http://ruby-doc.org/core-1.9/classes/Enumerable.html] def chunk(initial_state = nil, &original_block) raise ArgumentError, "no block given" unless block_given? ::Enumerator.new do |yielder| previous = nil accumulate = [] block = initial_state.nil? ? original_block : Proc.new{|val| original_block.yield(val, initial_state.clone)} each do |val| key = block.yield(val) if key.nil? || (key.is_a?(Symbol) && key.to_s[0,1] == "_") yielder.yield [previous, accumulate] unless accumulate.empty? accumulate = [] previous = nil case key when nil, :_separator when :_singleton yielder.yield [key, [val]] else raise RuntimeError, "symbol beginning with an underscore are reserved" end else if previous.nil? || previous == key accumulate << val else yielder.yield [previous, accumulate] unless accumulate.empty? accumulate = [val] end previous = key end end # what to do in case of a break? yielder.yield [previous, accumulate] unless accumulate.empty? end end unless method_defined? :chunk # Standard in Ruby 1.9.2 See official documentation[http://ruby-doc.org/core-1.9/classes/Enumerable.html] def flat_map(&block) return to_enum(:flat_map) unless block_given? map(&block).flatten(1) end unless method_defined? :flat_map alias_method :collect_concat, :flat_map unless method_defined? :collect_concat end
Version data entries
6 entries across 6 versions & 1 rubygems