In Ruby, these are equivalent:
At work today, we discussed having something similar for hash access. I came up with this:
I’m very happy with that syntax; it’s quite clear what it does.
The implementation is minimal:
It could be even smaller if it didn’t support multiple keys, but it does – and not just symbols, either:
There’s no default
Array#to_proc, so this doesn’t collide with anything built into the language.
For a tiny hack, it’s pretty cool.