Hash#each_with_index

I know if I have named tuples I can “dereference” them (or whatever it’s called) ex: each_with_index

a = {"abc": "def", "name": "value"}
a.each_with_index{|first, second, index|
  puts first, second, index # works!
}

It would be sometimes convenient if we could do the same thing with other things like Hash#each_with_index or what not, to get the key, value, and index in one fell swoop.
Today it just yields:

$ cat test.cr
a = {3 => 4, 5 => 6}
a.each_with_index{|k, v, i|

}
$ crystal test.cr
Showing last frame. Use --error-trace for full trace.

In test.cr:2:18

 2 | a.each_with_index{|k, v, i|
                      ^
Error: too many block arguments (given 3, expected maximum 2)

Requiring splitting of the key/value afterward.

Anyway just a feature request for discussion.
Cheers!

a.each_with_index{|(k, v) , i|

Note the parentheses

4 Likes