summaryrefslogtreecommitdiffhomepage
path: root/mrbgems/mruby-enum-chain/mrblib
diff options
context:
space:
mode:
authorYukihiro "Matz" Matsumoto <[email protected]>2019-07-17 10:35:41 +0900
committerGitHub <[email protected]>2019-07-17 10:35:41 +0900
commitd605b72c1d6fa4564a0a5e88535504b6850463b5 (patch)
tree774fc0de56002abb3bb2b1c3387ff08f91876d17 /mrbgems/mruby-enum-chain/mrblib
parent2af92d0ebcbeca6d3d85a27c8193273080a63090 (diff)
parent9af3b7c6258de327218dd04e69d76ae68caf17b1 (diff)
downloadmruby-d605b72c1d6fa4564a0a5e88535504b6850463b5.tar.gz
mruby-d605b72c1d6fa4564a0a5e88535504b6850463b5.zip
Merge branch 'master' into i110/inspect-recursion
Diffstat (limited to 'mrbgems/mruby-enum-chain/mrblib')
-rw-r--r--mrbgems/mruby-enum-chain/mrblib/chain.rb60
1 files changed, 60 insertions, 0 deletions
diff --git a/mrbgems/mruby-enum-chain/mrblib/chain.rb b/mrbgems/mruby-enum-chain/mrblib/chain.rb
new file mode 100644
index 000000000..98515ea14
--- /dev/null
+++ b/mrbgems/mruby-enum-chain/mrblib/chain.rb
@@ -0,0 +1,60 @@
+##
+# chain.rb Enumerator::Chain class
+# See Copyright Notice in mruby.h
+
+module Enumerable
+ def chain(*args)
+ Enumerator::Chain.new(self, *args)
+ end
+
+ def +(other)
+ Enumerator::Chain.new(self, other)
+ end
+end
+
+class Enumerator
+ class Chain
+ include Enumerable
+
+ def initialize(*args)
+ @enums = args
+ end
+
+ def initialize_copy(orig)
+ @enums = orig.__copy_enums
+ end
+
+ def each(&block)
+ return to_enum unless block_given?
+
+ @enums.each { |e| e.each(&block) }
+
+ self
+ end
+
+ def size
+ @enums.reduce(0) do |a, e|
+ return nil unless e.respond_to?(:size)
+ a + e.size
+ end
+ end
+
+ def rewind
+ @enums.reverse_each do |e|
+ e.rewind if e.respond_to?(:rewind)
+ end
+
+ self
+ end
+
+ def inspect
+ "#<#{self.class}: #{@enums.inspect}>"
+ end
+
+ def __copy_enums
+ @enums.each_with_object([]) do |e, a|
+ a << e.clone
+ end
+ end
+ end
+end