ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:116483] [Ruby master Bug#20223] For simple objects each_cons appears to work as I expect, but for other objects it seems to fail to terminate iterations when I would expect.
@ 2024-01-29 16:48 mjflynt (Jeffrey Flynt) via ruby-core
  2024-01-29 16:54 ` [ruby-core:116484] " jeremyevans0 (Jeremy Evans) via ruby-core
  0 siblings, 1 reply; 2+ messages in thread
From: mjflynt (Jeffrey Flynt) via ruby-core @ 2024-01-29 16:48 UTC (permalink / raw
  To: ruby-core; +Cc: mjflynt (Jeffrey Flynt)

Issue #20223 has been reported by mjflynt (Jeffrey Flynt).

----------------------------------------
Bug #20223: For simple objects each_cons appears to work as I expect, but for other objects it seems to fail to terminate iterations when I would expect.
https://bugs.ruby-lang.org/issues/20223

* Author: mjflynt (Jeffrey Flynt)
* Status: Open
* Priority: Normal
* ruby -v: ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [aarch64-linux]
* Backport: 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN
----------------------------------------
Reproduce process:
``` ruby
p `ruby -v`
class X
include Enumerable
  attr_accessor :obj
  def initialize = @obj = (1..5).to_a
  def each(&block) = yield @obj.each(&block)
end

wtf = X.new

(1..5).each_cons(3) { |g| p g }
p '~' * 20
(1..5).to_a.each_cons(3) { |g| p g }
p '~' * 20
('a'..'e').each_cons(3) { |g| p g }
p '~' * 20

wtf.each_cons(3) { |g| p g }
```


Result of reproduce process
```
"ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [aarch64-linux]\n"
[1, 2, 3]
[2, 3, 4]
[3, 4, 5]
"~~~~~~~~~~~~~~~~~~~~"
[1, 2, 3]
[2, 3, 4]
[3, 4, 5]
"~~~~~~~~~~~~~~~~~~~~"
["a", "b", "c"]
["b", "c", "d"]
["c", "d", "e"]
"~~~~~~~~~~~~~~~~~~~~"
[1, 2, 3]
[2, 3, 4]
[3, 4, 5]
[4, 5, [1, 2, 3, 4, 5]]

[Done] exited with code=0 in 0.173 seconds
```


Expected result and the reason why you expect

I expect the results to not include the last line of output "[4, 5, [1, 2, 3, 4, 5]]" because my understanding is each_cons ends its iteration over the collection before it runs out of elements. In this case, this fourth row does not have enough elements to do three consecutive elements, but it is doing so anyway, appearing to use the original array as the third element before it completes. 



-- 
https://bugs.ruby-lang.org/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 2+ messages in thread

* [ruby-core:116484] [Ruby master Bug#20223] For simple objects each_cons appears to work as I expect, but for other objects it seems to fail to terminate iterations when I would expect.
  2024-01-29 16:48 [ruby-core:116483] [Ruby master Bug#20223] For simple objects each_cons appears to work as I expect, but for other objects it seems to fail to terminate iterations when I would expect mjflynt (Jeffrey Flynt) via ruby-core
@ 2024-01-29 16:54 ` jeremyevans0 (Jeremy Evans) via ruby-core
  0 siblings, 0 replies; 2+ messages in thread
From: jeremyevans0 (Jeremy Evans) via ruby-core @ 2024-01-29 16:54 UTC (permalink / raw
  To: ruby-core; +Cc: jeremyevans0 (Jeremy Evans)

Issue #20223 has been updated by jeremyevans0 (Jeremy Evans).

Status changed from Open to Rejected

Not a bug, you are doing `yield @obj.each(&block)`, and Array#each returns the receiver.

----------------------------------------
Bug #20223: For simple objects each_cons appears to work as I expect, but for other objects it seems to fail to terminate iterations when I would expect.
https://bugs.ruby-lang.org/issues/20223#change-106506

* Author: mjflynt (Jeffrey Flynt)
* Status: Rejected
* Priority: Normal
* ruby -v: ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [aarch64-linux]
* Backport: 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN
----------------------------------------
Reproduce process:
``` ruby
p `ruby -v`
class X
include Enumerable
  attr_accessor :obj
  def initialize = @obj = (1..5).to_a
  def each(&block) = yield @obj.each(&block)
end

wtf = X.new

(1..5).each_cons(3) { |g| p g }
p '~' * 20
(1..5).to_a.each_cons(3) { |g| p g }
p '~' * 20
('a'..'e').each_cons(3) { |g| p g }
p '~' * 20

wtf.each_cons(3) { |g| p g }
```


Result of reproduce process
```
"ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [aarch64-linux]\n"
[1, 2, 3]
[2, 3, 4]
[3, 4, 5]
"~~~~~~~~~~~~~~~~~~~~"
[1, 2, 3]
[2, 3, 4]
[3, 4, 5]
"~~~~~~~~~~~~~~~~~~~~"
["a", "b", "c"]
["b", "c", "d"]
["c", "d", "e"]
"~~~~~~~~~~~~~~~~~~~~"
[1, 2, 3]
[2, 3, 4]
[3, 4, 5]
[4, 5, [1, 2, 3, 4, 5]]

[Done] exited with code=0 in 0.173 seconds
```


Expected result and the reason why you expect

I expect the results to not include the last line of output "[4, 5, [1, 2, 3, 4, 5]]" because my understanding is each_cons ends its iteration over the collection before it runs out of elements. In this case, this fourth row does not have enough elements to do three consecutive elements, but it is doing so anyway, appearing to use the original array as the third element before it completes. 



-- 
https://bugs.ruby-lang.org/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-01-29 16:54 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-01-29 16:48 [ruby-core:116483] [Ruby master Bug#20223] For simple objects each_cons appears to work as I expect, but for other objects it seems to fail to terminate iterations when I would expect mjflynt (Jeffrey Flynt) via ruby-core
2024-01-29 16:54 ` [ruby-core:116484] " jeremyevans0 (Jeremy Evans) via ruby-core

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).