stdlib/builtin/enumerator.rbs in rbs-0.13.1 vs stdlib/builtin/enumerator.rbs in rbs-0.14.0

- old
+ new

@@ -1,71 +1,71 @@ # A class which allows both internal and external iteration. -# +# # An [Enumerator](Enumerator) can be created by the # following methods. -# +# # - Kernel\#to\_enum -# +# # - Kernel\#enum\_for -# +# # - [::new](Enumerator#method-c-new) -# +# # Most methods have two forms: a block form where the contents are # evaluated for each item in the enumeration, and a non-block form which # returns a new [Enumerator](Enumerator) wrapping the # iteration. -# +# # ```ruby # enumerator = %w(one two three).each # puts enumerator.class # => Enumerator -# +# # enumerator.each_with_object("foo") do |item, obj| # puts "#{obj}: #{item}" # end -# +# # # foo: one # # foo: two # # foo: three -# +# # enum_with_obj = enumerator.each_with_object("foo") # puts enum_with_obj.class # => Enumerator -# +# # enum_with_obj.each do |item, obj| # puts "#{obj}: #{item}" # end -# +# # # foo: one # # foo: two # # foo: three # ``` -# +# # This allows you to chain Enumerators together. For example, you can map # a list's elements to strings containing the index and the element as a # string via: -# +# # ```ruby # puts %w[foo bar baz].map.with_index { |w, i| "#{i}:#{w}" } # # => ["0:foo", "1:bar", "2:baz"] # ``` -# +# # An [Enumerator](Enumerator) can also be used as an # external iterator. For example, # [\#next](Enumerator#method-i-next) returns the next # value of the iterator or raises # [StopIteration](https://ruby-doc.org/core-2.6.3/StopIteration.html) if # the [Enumerator](Enumerator) is at the end. -# +# # ```ruby # e = [1,2,3].each # returns an enumerator object. # puts e.next # => 1 # puts e.next # => 2 # puts e.next # => 3 # puts e.next # raises StopIteration # ``` -# +# # You can use this to implement an internal iterator as follows: -# +# # ```ruby # def ext_each(e) # while true # begin # vs = e.next_values @@ -74,24 +74,24 @@ # end # y = yield(*vs) # e.feed y # end # end -# +# # o = Object.new -# +# # def o.each # puts yield # puts yield(1) # puts yield(1, 2) # 3 # end -# +# # # use o.each as an internal iterator directly. # puts o.each {|*x| puts x; [:b, *x] } # # => [], [:b], [1], [:b, 1], [1, 2], [:b, 1, 2], 3 -# +# # # convert o.each to an external iterator for # # implementing an internal iterator. # puts ext_each(o.to_enum) {|*x| puts x; [:b, *x] } # # => [], [:b], [1], [:b, 1], [1, 2], [:b, 1, 2], 3 # ``` @@ -110,21 +110,21 @@ # Returns the next object in the enumerator, and move the internal # position forward. When the position reached at the end, # [StopIteration](https://ruby-doc.org/core-2.6.3/StopIteration.html) is # raised. - # - # + # + # # ```ruby # a = [1,2,3] # e = a.to_enum # p e.next #=> 1 # p e.next #=> 2 # p e.next #=> 3 # p e.next #raises StopIteration # ``` - # + # # Note that enumeration sequence by `next` does not affect other # non-external enumeration methods, unless the underlying iteration # methods itself has side-effect, e.g. # [IO\#each\_line](https://ruby-doc.org/core-2.6.3/IO.html#method-i-each_line) # . @@ -132,14 +132,14 @@ # Returns the next object as an array in the enumerator, and move the # internal position forward. When the position reached at the end, # [StopIteration](https://ruby-doc.org/core-2.6.3/StopIteration.html) is # raised. - # + # # This method can be used to distinguish `yield` and `yield nil` . - # - # + # + # # ```ruby # o = Object.new # def o.each # yield # yield 1 @@ -157,31 +157,31 @@ # p e.next # p e.next # p e.next # p e.next # p e.next - # + # # ## yield args next_values next # # yield [] nil # # yield 1 [1] 1 # # yield 1, 2 [1, 2] [1, 2] # # yield nil [nil] nil # # yield [1, 2] [[1, 2]] [1, 2] # ``` - # + # # Note that `next_values` does not affect other non-external enumeration # methods unless underlying iteration method itself has side-effect, e.g. # [IO\#each\_line](https://ruby-doc.org/core-2.6.3/IO.html#method-i-each_line) # . def next_values: () -> ::Array[Elem] # Returns the next object in the enumerator, but doesn’t move the internal # position forward. If the position is already at the end, # [StopIteration](https://ruby-doc.org/core-2.6.3/StopIteration.html) is # raised. - # - # + # + # # ```ruby # a = [1,2,3] # e = a.to_enum # p e.next #=> 1 # p e.peek #=> 2 @@ -197,12 +197,12 @@ # [\#next\_values](Enumerator.downloaded.ruby_doc#method-i-next_values), # but doesn’t move the internal position forward. If the position is # already at the end, # [StopIteration](https://ruby-doc.org/core-2.6.3/StopIteration.html) is # raised. - # - # + # + # # ```ruby # o = Object.new # def o.each # yield # yield 1 @@ -219,16 +219,16 @@ # p e.peek_values # raises StopIteration # ``` def peek_values: () -> ::Array[Elem] # Rewinds the enumeration sequence to the beginning. - # + # # If the enclosed object responds to a “rewind” method, it is called. def rewind: () -> self # Returns the size of the enumerator, or `nil` if it can’t be calculated # lazily. - # + # # ```ruby # (1..100).to_a.permutation(4).size # => 94109400 # loop.size # => Float::INFINITY # (1..100).drop_while.size # => nil # ```