Search code examples
rubyblockenumerators

Chaining enumerators that yield multiple arguments


I'm trying to figure out how Ruby handles chaining enumerators that yield multiple arguments. Take a look at this snippet:

a = ['a', 'b', 'c']

a.each_with_index.select{|pr| p pr}
# prints:
# ["a", 0]
# ["b", 1]
# ["c", 2]

a.each_with_index.map{|pr| p pr}
# prints:
# "a"
# "b"
# "c"

Why does select yield the arguments as an array, whereas map yields them as two separate arguments?


Solution

  • From the discourse so far, it follows that we can analyze the source code, but we do not know the whys. Ruby core team is relatively very responsive. I recommend you to sign in at http://bugs.ruby-lang.org/issues/ and post a bug report there. They will surely look at this issue at most within a few weeks, and you can probably expect it corrected in the next minor version of Ruby. (That is, unless there is a design rationale unknown to us to keep things as they are.)