From: Vimlesh Patel Date: 2014-11-21T17:20:05+05:30 Subject: [ruby-core:66390] Re: [ruby-trunk - Bug #10534] [Open] Enumerator methods other than "next" do not respect "peek" --001a11c324d4bf424405085d0b5f Content-Type: text/plain; charset=ISO-8859-1 If I'm changing the sequence of iterator calling like > enum = StringIO.new("foo").each => #:each> > enum.peek => "foo" > enum = StringIO.new("foo").each => #:each> > enum.to_a => ["foo"] > enum.peek then it showing this error: StopIteration: iteration reached an end from (irb):10:in `peek' from (irb):10 from /usr/local/rvm/gems/ruby-2.1.1@rails4/gems/railties-4.0.8/lib/rails/commands/console.rb:90:in `start' from /usr/local/rvm/gems/ruby-2.1.1@rails4/gems/railties-4.0.8/lib/rails/commands/console.rb:9:in `start' from /usr/local/rvm/gems/ruby-2.1.1@rails4/gems/railties-4.0.8/lib/rails/commands.rb:62:in `' from bin/rails:4:in `require' from bin/rails:4:in `
' On Fri, Nov 21, 2014 at 5:08 PM, wrote: > Issue #10534 has been reported by Jonas Nicklas. > > ---------------------------------------- > Bug #10534: Enumerator methods other than "next" do not respect "peek" > https://bugs.ruby-lang.org/issues/10534 > > * Author: Jonas Nicklas > * Status: Open > * Priority: Normal > * Assignee: Yukihiro Matsumoto > * Category: core > * Target version: > * ruby -v: ruby 2.1.5p273 (2014-11-13 revision 48405) [x86_64-darwin14.0] > * Backport: 2.0.0: UNKNOWN, 2.1: UNKNOWN > ---------------------------------------- > It seems like using "peek" and then calling other methods on the > Enumerator consumes the value that was peeked at. While `next` correctly > returns the peeked-at value the next time it is called, other Enumerator > methods such as `to_a` and `each` do not. > > ~~~ > >> enum = StringIO.new("foo").each > => #:each> > >> enum.peek > => "foo" > >> enum.to_a > => [] > ~~~ > > Here the final call to `enum.to_a` should have returned `["foo"]`, since > we have not consumed anything from the enumerator yet. The peeked-at value > is not included in the returned Array. > > Taking a glance at the code, it seems that these methods do not call > `next` under the hood, but instead have some other mechanism of iterating > over the Enumerator, this seems very counter-intuitive to me. > > > > -- > https://bugs.ruby-lang.org/ > -- Vimlesh Kumar Patel --001a11c324d4bf424405085d0b5f Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
If I'm changing the sequence of iterator= calling like
=A0> =A0en= um =3D StringIO.new("foo").each
=A0=3D> #<Enumerator: #<StringIO:0x00000003822078>:each>=A0=
=A0> enum.peek
=A0=3D> "foo"=A0
> =A0enum =3D StringIO.new("foo").each
=A0=3D> #<Enumerator: #<StringIO:0x000000037e6140>:= each>=A0
> enum.to_a
=A0=3D> ["foo"]=A0
> enum.peek
then it showing this err= or:
StopIteration: iteratio= n reached an end
=A0 =A0 =A0 =A0 from (ir= b):10:in `peek'
=A0 =A0 =A0 =A0 from = (irb):10
=A0 =A0 =A0 =A0 from /usr/local/= rvm/gems/ruby-2.1.1@rails4/gems/railties-4.0.8/lib/rails/commands/console.r= b:90:in `start'
=A0 =A0 =A0 =A0 from = /usr/local/rvm/gems/ruby-2.1.1@rails4/gems/railties-4.0.8/lib/rails/command= s/console.rb:9:in `start'
=A0 =A0 =A0= =A0 from /usr/local/rvm/gems/ruby-2.1.1@rails4/gems/railties-4.0.8/lib/rai= ls/commands.rb:62:in `<top (required)>'
=A0 =A0 =A0 =A0 from bin/rails:4:in `require'
=A0 =A0 =A0 =A0 from bin/rails:4:in `<main>'

O= n Fri, Nov 21, 2014 at 5:08 PM, <jonas.nicklas@gmail.com> wrote:
Issue #10534 has been reporte= d by Jonas Nicklas.

----------------------------------------
Bug #10534: Enumerator methods other than "next" do not respect &= quot;peek"
https= ://bugs.ruby-lang.org/issues/10534

* Author: Jonas Nicklas
* Status: Open
* Priority: Normal
* Assignee: Yukihiro Matsumoto
* Category: core
* Target version:
* ruby -v: ruby 2.1.5p273 (2014-11-13 revision 48405) [x86_64-darwin14.0] * Backport: 2.0.0: UNKNOWN, 2.1: UNKNOWN
----------------------------------------
It seems like using "peek" and then calling other methods on the = Enumerator consumes the value that was peeked at. While `next` correctly re= turns the peeked-at value the next time it is called, other Enumerator meth= ods such as `to_a` and `each` do not.

~~~
>> enum =3D StringIO.new("foo").each
=3D> #<Enumerator: #<StringIO:0x007ff3313d8688>:each>
>> enum.peek
=3D> "foo"
>> enum.to_a
=3D> []
~~~

Here the final call to `enum.to_a` should have returned `["foo"]`= , since we have not consumed anything from the enumerator yet. The peeked-a= t value is not included in the returned Array.

Taking a glance at the code, it seems that these methods do not call `next`= under the hood, but instead have some other mechanism of iterating over th= e Enumerator, this seems very counter-intuitive to me.



--
https://bugs.ruby= -lang.org/



--
Vimlesh Kumar Patel
--001a11c324d4bf424405085d0b5f--