Skip to content

Commit 3b756ce

Browse files
committed
[GR-20313] Implement Enumerator::Chain#{initialize, initialize_copy, each, size, rewind, inspect}, Enumerator#+, and Enumerable#chain (#1859, #1858).
PullRequest: truffleruby/1209
2 parents 6e08974 + 15993d1 commit 3b756ce

File tree

11 files changed

+54
-21
lines changed

11 files changed

+54
-21
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ Compatibility:
7171
* Implemented `rb_gc_register_mark_object` and `rb_enc_str_asciionly_p` (#1856, @chrisseaton).
7272
* Implemented `rb_io_set_nonblock` (#1741).
7373
* Include the major kernel version in `RUBY_PLATFORM` on macOS like MRI (#1860, @eightbitraptor).
74+
* Implemented `Enumerator::Chain`, `Enumerator#+`, and `Enumerable#chain` (#1859, #1858).
7475

7576
Performance:
7677

spec/tags/core/enumerable/chain_tags.txt

Lines changed: 0 additions & 2 deletions
This file was deleted.

spec/tags/core/enumerator/chain/each_tags.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

spec/tags/core/enumerator/chain/initialize_tags.txt

Lines changed: 0 additions & 4 deletions
This file was deleted.

spec/tags/core/enumerator/chain/inspect_tags.txt

Lines changed: 0 additions & 2 deletions
This file was deleted.

spec/tags/core/enumerator/chain/rewind_tags.txt

Lines changed: 0 additions & 6 deletions
This file was deleted.

spec/tags/core/enumerator/chain/size_tags.txt

Lines changed: 0 additions & 2 deletions
This file was deleted.

spec/tags/core/enumerator/plus_tags.txt

Lines changed: 0 additions & 2 deletions
This file was deleted.

src/main/ruby/truffleruby/core/enumerable.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -986,6 +986,10 @@ def sum(init = 0)
986986
end
987987
end
988988

989+
def chain(*enums)
990+
Enumerator::Chain.new(self, *enums)
991+
end
992+
989993
end
990994

991995
class Array

src/main/ruby/truffleruby/core/enumerator.rb

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,10 @@ def feed(val)
209209
nil
210210
end
211211

212+
def +(other)
213+
Enumerator::Chain.new(self, other)
214+
end
215+
212216
class Yielder
213217
def initialize(&block)
214218
raise LocalJumpError, 'Expected a block to be given' unless block_given?
@@ -561,4 +565,49 @@ class Enumerator::ArithmeticSequence < Enumerator
561565
end
562566

563567
class Enumerator::Chain < Enumerator
568+
def initialize(*args, &block)
569+
TrufflePrimitive.check_frozen self
570+
@enums = args.freeze
571+
@pos = -1
572+
self
573+
end
574+
575+
def each
576+
return to_enum :each unless block_given?
577+
@enums.each_with_index do |enum, idx|
578+
@pos = idx
579+
enum.each do |*args|
580+
yield(*args)
581+
end
582+
end
583+
end
584+
585+
def size
586+
total = 0
587+
@enums.each do |e|
588+
size = e.size
589+
if size.nil? || (size.is_a?(Float) && size.infinite?)
590+
return size
591+
end
592+
unless size.is_a?(Integer)
593+
return nil
594+
end
595+
total += size
596+
end
597+
total
598+
end
599+
600+
def rewind
601+
while @pos >= 0
602+
Truffle::Type.check_funcall(@enums[@pos], :rewind)
603+
@pos -= 1
604+
end
605+
self
606+
end
607+
608+
def inspect
609+
return "#<#{self.class.name}: ...>" if Thread.detect_recursion(self) do
610+
return "#<#{self.class.name}: #{@enums}>"
611+
end
612+
end
564613
end

0 commit comments

Comments
 (0)