Module: Enumerable
- Included in:
- Prime, Prime, Prime::PseudoPrimeGenerator
- Defined in:
- lib/backports/rails/enumerable.rb,
lib/backports/1.8.7/enumerable/one.rb,
lib/backports/2.4.0/enumerable/sum.rb,
lib/backports/2.5.0/enumerable/all.rb,
lib/backports/2.5.0/enumerable/any.rb,
lib/backports/2.5.0/enumerable/one.rb,
lib/backports/1.8.7/enumerable/drop.rb,
lib/backports/1.8.7/enumerable/none.rb,
lib/backports/1.8.7/enumerable/take.rb,
lib/backports/1.8.7/enumerable/to_a.rb,
lib/backports/2.0.0/enumerable/lazy.rb,
lib/backports/2.1.0/enumerable/to_h.rb,
lib/backports/2.4.0/enumerable/uniq.rb,
lib/backports/2.5.0/enumerable/none.rb,
lib/backports/2.6.0/enumerable/to_h.rb,
lib/backports/1.8.7/enumerable/count.rb,
lib/backports/1.8.7/enumerable/cycle.rb,
lib/backports/1.8.7/enumerable/first.rb,
lib/backports/1.9.2/enumerable/chunk.rb,
lib/backports/2.6.0/enumerable/chain.rb,
lib/backports/2.7.0/enumerable/tally.rb,
lib/backports/3.1.0/enumerable/tally.rb,
lib/backports/1.8.7/enumerable/inject.rb,
lib/backports/1.8.7/enumerable/max_by.rb,
lib/backports/1.8.7/enumerable/min_by.rb,
lib/backports/1.8.7/enumerable/minmax.rb,
lib/backports/2.3.0/enumerable/grep_v.rb,
lib/backports/1.8.7/enumerable/entries.rb,
lib/backports/3.1.0/enumerable/compact.rb,
lib/backports/1.8.7/enumerable/group_by.rb,
lib/backports/1.9.2/enumerable/flat_map.rb,
lib/backports/1.8.7/enumerable/minmax_by.rb,
lib/backports/1.8.7/enumerable/drop_while.rb,
lib/backports/1.8.7/enumerable/find_index.rb,
lib/backports/1.8.7/enumerable/take_while.rb,
lib/backports/1.9.2/enumerable/each_entry.rb,
lib/backports/2.2.0/enumerable/slice_when.rb,
lib/backports/2.7.0/enumerable/filter_map.rb,
lib/backports/2.2.0/enumerable/slice_after.rb,
lib/backports/2.3.0/enumerable/chunk_while.rb,
lib/backports/1.8.7/enumerable/reverse_each.rb,
lib/backports/1.9.2/enumerable/slice_before.rb,
lib/backports/1.9.1/enumerable/each_with_index.rb,
lib/backports/1.9.1/enumerable/each_with_object.rb
Defined Under Namespace
Classes: Enumerator
Instance Method Summary collapse
- #all_with_pattern?(pattern = Backports::Undefined, &block) ⇒ Boolean
- #any_with_pattern?(pattern = Backports::Undefined, &block) ⇒ Boolean
- #chain(*enums) ⇒ Object
- #chunk(initial_state = nil, &original_block) ⇒ Object
- #chunk_while(&block) ⇒ Object
- #compact ⇒ Object
- #count(item = Backports::Undefined) ⇒ Object
- #cycle(n = nil) ⇒ Object
- #drop(n) ⇒ Object
-
#drop_while ⇒ Object
Standard in Ruby 1.8.7+.
- #each_entry(*pass) ⇒ Object
- #each_with_index_with_optional_args_and_block(*args) ⇒ Object
- #each_with_object(memo) ⇒ Object
- #entries_with_optional_arguments(*args) ⇒ Object
- #filter_map ⇒ Object
- #find_index(obj = Backports::Undefined) ⇒ Object
- #first(n = Backports::Undefined) ⇒ Object
- #flat_map ⇒ Object (also: #collect_concat)
- #grep_v(pattern) ⇒ Object
- #group_by ⇒ Object
- #inject_with_symbol(*args, &block) ⇒ Object
- #lazy ⇒ Object
- #max_by ⇒ Object
- #min_by ⇒ Object
- #minmax ⇒ Object
-
#minmax_by ⇒ Object
Standard in Ruby 1.8.7+.
- #none?(&block) ⇒ Boolean
- #none_with_pattern?(pattern = Backports::Undefined, &block) ⇒ Boolean
- #one? ⇒ Boolean
- #one_with_pattern?(pattern = Backports::Undefined, &block) ⇒ Boolean
- #reverse_each ⇒ Object
- #slice_after(pattern = Backports::Undefined, &block) ⇒ Object
- #slice_before(arg = Backports::Undefined, &block) ⇒ Object
- #slice_when(&block) ⇒ Object
-
#sum(accumulator = 0, &block) ⇒ Object
Standard in rails…
- #take(n) ⇒ Object
-
#take_while ⇒ Object
Standard in Ruby 1.8.7+.
- #tally ⇒ Object
- #tally_with_hash_argument(h = ::Backports::Undefined) ⇒ Object
- #to_a_with_optional_arguments(*args) ⇒ Object
- #to_h(*args) ⇒ Object
- #to_h_with_block(*args, &block) ⇒ Object
- #uniq(&block) ⇒ Object
Instance Method Details
#all_with_pattern?(pattern = Backports::Undefined, &block) ⇒ Boolean
6 7 8 9 10 |
# File 'lib/backports/2.5.0/enumerable/all.rb', line 6 def all_with_pattern?(pattern = Backports::Undefined, &block) return all_without_pattern?(&block) if Backports::Undefined == pattern each_entry { |x| return false unless pattern === x } true end |
#any_with_pattern?(pattern = Backports::Undefined, &block) ⇒ Boolean
6 7 8 9 10 |
# File 'lib/backports/2.5.0/enumerable/any.rb', line 6 def any_with_pattern?(pattern = Backports::Undefined, &block) return any_without_pattern?(&block) if Backports::Undefined == pattern each_entry { |x| return true if pattern === x } false end |
#chain(*enums) ⇒ Object
3 4 5 |
# File 'lib/backports/2.6.0/enumerable/chain.rb', line 3 def chain(*enums) Enumerator::Chain.new(self, *enums) end |
#chunk(initial_state = nil, &original_block) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/backports/1.9.2/enumerable/chunk.rb', line 5 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.call(val, initial_state.clone)} each do |val| key = block.call(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 :_alone 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 |
#chunk_while(&block) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/backports/2.3.0/enumerable/chunk_while.rb', line 6 def chunk_while(&block) raise ArgumentError, 'tried to create Proc object without a block' unless block enum = self Enumerator.new do |y| acc = [] prev = Backports::Undefined enum.each do |*elem| elem = elem.first if elem.length == 1 unless prev == Backports::Undefined unless block.call(prev, elem) y.yield acc acc = [] end end acc << elem prev = elem end y.yield acc unless acc.empty? end end |
#compact ⇒ Object
2 3 4 |
# File 'lib/backports/3.1.0/enumerable/compact.rb', line 2 def compact reject { |elem| nil == elem } end |
#count(item = Backports::Undefined) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 |
# File 'lib/backports/1.8.7/enumerable/count.rb', line 5 def count(item = Backports::Undefined) seq = 0 if item != Backports::Undefined each { |o| seq += 1 if item == o } elsif block_given? each { |o| seq += 1 if yield(o) } else each { seq += 1 } end seq end |
#cycle(n = nil) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/backports/1.8.7/enumerable/cycle.rb', line 6 def cycle(n = nil) return to_enum(:cycle, n) unless block_given? n = n && Backports.coerce_to_int(n) if n == nil || n >= 1 cache = [] each do |elem| cache << elem yield elem end if n (n-1).times { cache.each{|e| yield e } } else loop { cache.each{|e| yield e } } end unless cache.empty? end nil end |
#drop(n) ⇒ Object
5 6 7 8 9 10 11 |
# File 'lib/backports/1.8.7/enumerable/drop.rb', line 5 def drop(n) n = Backports.coerce_to_int(n) raise ArgumentError, "attempt to drop negative size" if n < 0 ary = to_a return [] if n > ary.size ary[n...ary.size] end |
#drop_while ⇒ Object
Standard in Ruby 1.8.7+. See official documentation
6 7 8 9 10 11 12 13 14 |
# File 'lib/backports/1.8.7/enumerable/drop_while.rb', line 6 def drop_while return to_enum(:drop_while) unless block_given? ary = [] dropping = true each do |obj| ary << obj unless dropping &&= yield(obj) end ary end |
#each_entry(*pass) ⇒ Object
3 4 5 6 7 8 9 |
# File 'lib/backports/1.9.2/enumerable/each_entry.rb', line 3 def each_entry(*pass) return to_enum(:each_entry, *pass) unless block_given? each(*pass) do |*args| yield args.size == 1 ? args[0] : args end self end |
#each_with_index_with_optional_args_and_block(*args) ⇒ Object
6 7 8 9 10 11 |
# File 'lib/backports/1.9.1/enumerable/each_with_index.rb', line 6 def each_with_index_with_optional_args_and_block(*args) return to_enum(:each_with_index, *args) unless block_given? idx = 0 each(*args) { |o| yield(o, idx); idx += 1 } self end |
#each_with_object(memo) ⇒ Object
3 4 5 6 7 |
# File 'lib/backports/1.9.1/enumerable/each_with_object.rb', line 3 def each_with_object(memo) return to_enum(:each_with_object, memo) unless block_given? each {|obj| yield obj, memo} memo end |
#entries_with_optional_arguments(*args) ⇒ Object
5 6 7 8 |
# File 'lib/backports/1.8.7/enumerable/entries.rb', line 5 def entries_with_optional_arguments(*args) return entries_without_optional_arguments if args.empty? to_enum(:each, *args).entries end |
#filter_map ⇒ Object
5 6 7 8 9 10 11 12 |
# File 'lib/backports/2.7.0/enumerable/filter_map.rb', line 5 def filter_map return to_enum(:filter_map) unless block_given? each_with_object([]) do |item, res| processed = yield(item) res << processed if processed end end |
#find_index(obj = Backports::Undefined) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/backports/1.8.7/enumerable/find_index.rb', line 6 def find_index(obj = Backports::Undefined) if obj != Backports::Undefined each_with_index do |element, i| return i if element == obj end elsif block_given? each_with_index do |element, i| return i if yield element end else return to_enum(:find_index) end nil end |
#first(n = Backports::Undefined) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/backports/1.8.7/enumerable/first.rb', line 5 def first(n = Backports::Undefined) if n == Backports::Undefined each{|obj| return obj} nil else n = Backports.coerce_to_int(n) raise ArgumentError, "attempt to take negative size: #{n}" if n < 0 array = [] each do |elem| array << elem break if array.size >= n end unless n == 0 array end end |
#flat_map ⇒ Object Also known as: collect_concat
3 4 5 6 7 8 9 10 11 |
# File 'lib/backports/1.9.2/enumerable/flat_map.rb', line 3 def flat_map return to_enum(:flat_map) unless block_given? r = [] each do |*args| result = yield(*args) result.respond_to?(:to_ary) ? r.concat(result) : r.push(result) end r end |
#grep_v(pattern) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 |
# File 'lib/backports/2.3.0/enumerable/grep_v.rb', line 5 def grep_v(pattern) if block_given? acc = [] each_entry do |v| acc << yield(v) unless pattern === v end acc else reject {|v| pattern === v } end end |
#group_by ⇒ Object
5 6 7 8 9 10 11 12 |
# File 'lib/backports/1.8.7/enumerable/group_by.rb', line 5 def group_by return to_enum(:group_by) unless block_given? result = {} each do |o| result.fetch(yield(o)){|key| result[key] = []} << o end result end |
#inject_with_symbol(*args, &block) ⇒ Object
6 7 8 9 10 |
# File 'lib/backports/1.8.7/enumerable/inject.rb', line 6 def inject_with_symbol(*args, &block) return inject_without_symbol(*args, &block) if block_given? && args.size <= 1 method = args.pop inject_without_symbol(*args) {|memo, obj| memo.send(method, obj)} end |
#lazy ⇒ Object
6 7 8 9 |
# File 'lib/backports/2.0.0/enumerable/lazy.rb', line 6 def lazy klass = Enumerator::Lazy.send :class_variable_get, :@@lazy_with_no_block # Note: class_variable_get is private in 1.8 Enumerator::Lazy.new(klass.new(self, :each, [])) end |
#max_by ⇒ Object
6 7 8 9 10 11 12 13 14 |
# File 'lib/backports/1.8.7/enumerable/max_by.rb', line 6 def max_by return to_enum(:max_by) unless block_given? max_object, max_result = nil, Backports::MOST_EXTREME_OBJECT_EVER each do |object| result = yield object max_object, max_result = object, result if max_result < result end max_object end |
#min_by ⇒ Object
6 7 8 9 10 11 12 13 14 |
# File 'lib/backports/1.8.7/enumerable/min_by.rb', line 6 def min_by return to_enum(:min_by) unless block_given? min_object, min_result = nil, Backports::MOST_EXTREME_OBJECT_EVER each do |object| result = yield object min_object, min_result = object, result if min_result > result end min_object end |
#minmax ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/backports/1.8.7/enumerable/minmax.rb', line 5 def minmax return minmax{|a,b| a <=> b} unless block_given? first_time = true min, max = nil each do |object| if first_time min = max = object first_time = false else min = object if Backports.coerce_to_comparison(min, object, yield(min, object)) > 0 max = object if Backports.coerce_to_comparison(max, object, yield(max, object)) < 0 end end [min, max] end |
#minmax_by ⇒ Object
Standard in Ruby 1.8.7+. See official documentation
7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/backports/1.8.7/enumerable/minmax_by.rb', line 7 def minmax_by return to_enum(:minmax_by) unless block_given? min_object, min_result = nil, Backports::MOST_EXTREME_OBJECT_EVER max_object, max_result = nil, Backports::MOST_EXTREME_OBJECT_EVER each do |object| result = yield object min_object, min_result = object, result if min_result > result max_object, max_result = object, result if max_result < result end [min_object, max_object] end |
#none?(&block) ⇒ Boolean
3 4 5 |
# File 'lib/backports/1.8.7/enumerable/none.rb', line 3 def none?(&block) !any?(&block) end |
#none_with_pattern?(pattern = Backports::Undefined, &block) ⇒ Boolean
8 9 10 11 12 |
# File 'lib/backports/2.5.0/enumerable/none.rb', line 8 def none_with_pattern?(pattern = Backports::Undefined, &block) return none_without_pattern?(&block) if Backports::Undefined == pattern each_entry { |x| return false if pattern === x } true end |
#one? ⇒ Boolean
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/backports/1.8.7/enumerable/one.rb', line 3 def one? found_one = false if block_given? each do |o| if yield(o) return false if found_one found_one = true end end else each do |o| if o return false if found_one found_one = true end end end found_one end |
#one_with_pattern?(pattern = Backports::Undefined, &block) ⇒ Boolean
8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/backports/2.5.0/enumerable/one.rb', line 8 def one_with_pattern?(pattern = Backports::Undefined, &block) return one_without_pattern?(&block) if Backports::Undefined == pattern found_one = false each_entry do |o| if pattern === o return false if found_one found_one = true end end found_one end |
#reverse_each ⇒ Object
5 6 7 8 9 10 |
# File 'lib/backports/1.8.7/enumerable/reverse_each.rb', line 5 def reverse_each return to_enum(:reverse_each) unless block_given? # There is no other way then to convert to an array first... see 1.9's source. to_a.reverse_each{|e| yield e} self end |
#slice_after(pattern = Backports::Undefined, &block) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/backports/2.2.0/enumerable/slice_after.rb', line 6 def slice_after(pattern = Backports::Undefined, &block) raise ArgumentError, 'both pattern and block are given' if pattern != Backports::Undefined && block raise ArgumentError, 'wrong number of arguments (given 0, expected 1)' if pattern == Backports::Undefined && !block enum = self block ||= proc {|elem| pattern === elem} Enumerator.new do |y| acc = [] enum.each do |*elem| elem = elem.first if elem.length == 1 acc << elem if block.call(elem) y.yield acc acc = [] end end y.yield acc unless acc.empty? end end |
#slice_before(arg = Backports::Undefined, &block) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/backports/1.9.2/enumerable/slice_before.rb', line 6 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 |
#slice_when(&block) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/backports/2.2.0/enumerable/slice_when.rb', line 6 def slice_when(&block) raise ArgumentError, 'tried to create Proc object without a block' unless block enum = self Enumerator.new do |y| acc = [] prev = Backports::Undefined enum.each do |*elem| elem = elem.first if elem.length == 1 unless prev == Backports::Undefined if block.call(prev, elem) y.yield acc acc = [] end end acc << elem prev = elem end y.yield acc unless acc.empty? end end |
#sum(accumulator = 0, &block) ⇒ Object
Standard in rails… See official documentation Modified from rails 2.3 to not rely on size
8 9 10 11 12 13 14 |
# File 'lib/backports/rails/enumerable.rb', line 8 def sum(identity = 0, &block) if block_given? map(&block).sum(identity) else inject { |sum, element| sum + element } || identity end end |
#take(n) ⇒ Object
5 6 7 |
# File 'lib/backports/1.8.7/enumerable/take.rb', line 5 def take(n) first(n) end |
#take_while ⇒ Object
Standard in Ruby 1.8.7+. See official documentation
6 7 8 9 10 11 12 |
# File 'lib/backports/1.8.7/enumerable/take_while.rb', line 6 def take_while return to_enum(:take_while) unless block_given? inject([]) do |array, elem| return array unless yield elem array << elem end end |
#tally ⇒ Object
3 4 5 6 7 8 9 |
# File 'lib/backports/2.7.0/enumerable/tally.rb', line 3 def tally h = {} # NB: By spec, tally should return default-less hash each_entry { |item| h[item] = h.fetch(item, 0) + 1 } h end |
#tally_with_hash_argument(h = ::Backports::Undefined) ⇒ Object
7 8 9 10 11 12 13 14 15 |
# File 'lib/backports/3.1.0/enumerable/tally.rb', line 7 def tally_with_hash_argument(h = ::Backports::Undefined) return tally_without_hash_argument if h.equal? ::Backports::Undefined h = ::Backports.coerce_to_hash(h) each_entry { |item| h[item] = h.fetch(item, 0) + 1 } h end |
#to_a_with_optional_arguments(*args) ⇒ Object
6 7 8 9 |
# File 'lib/backports/1.8.7/enumerable/to_a.rb', line 6 def to_a_with_optional_arguments(*args) return to_a_without_optional_arguments if args.empty? to_enum(:each, *args).to_a end |
#to_h(*args) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/backports/2.1.0/enumerable/to_h.rb', line 6 def to_h(*args) h = {} each_entry(*args) do |key_value| key_value = Backports.coerce_to_ary(key_value) if key_value.size != 2 raise ArgumentError, "element has wrong array length (expected 2, was #{key_value.size})" end h[ key_value[0] ] = key_value[1] end h end |
#to_h_with_block(*args, &block) ⇒ Object
7 8 9 10 |
# File 'lib/backports/2.6.0/enumerable/to_h.rb', line 7 def to_h_with_block(*args, &block) return to_h_without_block(*args) unless block map(*args, &block).to_h_without_block end |
#uniq(&block) ⇒ Object
2 3 4 |
# File 'lib/backports/2.4.0/enumerable/uniq.rb', line 2 def uniq(&block) to_a.uniq(&block) end |