Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calling with_index with a block on an enumerator results in the wrong output type #2150

Open
herwinw opened this issue Jun 26, 2024 · 1 comment · May be fixed by #2153
Open

Calling with_index with a block on an enumerator results in the wrong output type #2150

herwinw opened this issue Jun 26, 2024 · 1 comment · May be fixed by #2153
Assignees
Labels
bug Something isn't working

Comments

@herwinw
Copy link
Member

herwinw commented Jun 26, 2024

Observed in #2145 where it broke our self hosted compiler. This snippet:

p [:a].map.class
p [:a].map.with_index.class
p [:a].map.with_index { |value, idx| [value, idx] }.class
p [:a].map.with_index { |value, idx| [value, idx] }.to_a.class
p [:a].map.with_index { |value, idx| [value, idx] }
p [:a].map.with_index { |value, idx| [value, idx] }.to_a

The output of MRI (I used version 3.3.3, but it should be the same in any supported version):

Enumerator
Enumerator
Array
Array
[[:a, 0]]
[[:a, 0]]

The output of the same program ran with Natalie:

Enumerator
Enumerator
Enumerator
Array
#<Enumerator:0x564692b83c60>
[:a]

The call to Enumerator#with_index is supposed to return a new Enumerator if no block is given, but should return an array if a block is given.
Additionally, the output of [:a].map.with_index { |value, idx| [value, idx] }.to_a is the original array, not the mapped version.

@herwinw herwinw added the bug Something isn't working label Jun 26, 2024
@herwinw herwinw self-assigned this Jun 26, 2024
@herwinw
Copy link
Member Author

herwinw commented Jun 26, 2024

p [:a].each_with_index.map { |value, idx| [value, idx] }

This appears to be working, so this bug is probably specific for with_index and not a generic bug. I've used this one as a mitigation for the issue in #2145, I have to admit this one is a bit more readable as well.

@herwinw herwinw linked a pull request Jun 26, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant