class Enumerator
Constants
- SPINNER
Public Instance Methods
Multiplies this Enumerator
by something else.
Enumerator
* Integer
== a new Enumerator
that repeats the original one <Integer> times Enumerator
* String
== joins the Enumerator's elements into a new string, with <String> in between each pair of elements Enumerator
* Enumerable
== the cross product (aka. cartesian product) of the Enumerator
and the Enumerable
# File lib/epitools/core_ext/enumerable.rb, line 621 def *(other) case other when Integer cycle(other) when String join(other) when Enumerable cross(other) else raise "#{other.class} is not something that can be multiplied by an Enumerator" end end
Concatenates two Enumerators, returning a new Enumerator
.
# File lib/epitools/core_ext/enumerable.rb, line 604 def +(other) raise "Can only concatenate Enumerable things to Enumerators" unless Enumerable === other Enumerator.new(count + other.count) do |yielder| each { |e| yielder << e } other.each { |e| yielder << e } end end
Takes the cross product (aka. cartesian product) of the Enumerator
and the argument, returning a new Enumerator
. (The argument must be some kind of Enumerable
.)
# File lib/epitools/core_ext/enumerable.rb, line 638 def cross_product(other) Enumerator.new(count + other.count) do |yielder| each { |a| other.each { |b| yielder << [a,b] } } end end
Pass in a bunch of indexes to elements in the Enumerator
, and this method lazily returns them as a new Enumerator
.
# File lib/epitools/core_ext/enumerable.rb, line 584 def values_at(*indexes) return if indexes.empty? indexes.flatten! indexes = Set.new(indexes) Enumerator.new do |yielder| each_with_index do |e,i| yielder << e if indexes.delete(i) break if indexes.empty? end end end
Display a spinner every `every` elements that pass through the Enumerator
.
# File lib/epitools/core_ext/enumerable.rb, line 560 def with_spinner(every=37) to_enum do |yielder| spins = 0 each.with_index do |e, i| if i % every == 0 print "\b" unless spins == 0 print SPINNER[spins % 4] spins += 1 end yielder << e end print "\b \b" # erase the spinner when done end end