Class: Enumerator
Constant Summary collapse
- SPINNER =
['-', '\\', '|', '/']
Instance Method Summary collapse
-
#*(other) ⇒ Object
Multiplies this Enumerator by something else.
-
#+(other) ⇒ Object
Concatenates two Enumerators, returning a new Enumerator.
-
#cross_product(other) ⇒ Object
(also: #cross)
Takes the cross product (aka. cartesian product) of the Enumerator and the argument, returning a new Enumerator.
-
#values_at(*indexes) ⇒ Object
Pass in a bunch of indexes to elements in the Enumerator, and this method lazily returns them as a new Enumerator.
-
#with_spinner(every = 37) ⇒ Object
Display a spinner every ‘every` elements that pass through the Enumerator.
Instance Method Details
#*(other) ⇒ Object
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
506 507 508 509 510 511 512 513 514 515 516 517 |
# File 'lib/epitools/core_ext/enumerable.rb', line 506 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 |
#+(other) ⇒ Object
Concatenates two Enumerators, returning a new Enumerator.
489 490 491 492 493 494 495 496 |
# File 'lib/epitools/core_ext/enumerable.rb', line 489 def +(other) raise "Can only concatenate Enumerable things to Enumerators" unless Enumerable === other Enumerator.new do |yielder| each { |e| yielder << e } other.each { |e| yielder << e } end end |
#cross_product(other) ⇒ Object Also known as: cross
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.)
523 524 525 526 527 |
# File 'lib/epitools/core_ext/enumerable.rb', line 523 def cross_product(other) Enumerator.new do |yielder| each { |a| other.each { |b| yielder << [a,b] } } end end |
#values_at(*indexes) ⇒ Object
Pass in a bunch of indexes to elements in the Enumerator, and this method lazily returns them as a new Enumerator.
469 470 471 472 473 474 475 476 477 478 479 480 481 482 |
# File 'lib/epitools/core_ext/enumerable.rb', line 469 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 |
#with_spinner(every = 37) ⇒ Object
Display a spinner every ‘every` elements that pass through the Enumerator.
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 |
# File 'lib/epitools/core_ext/enumerable.rb', line 445 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 |