So a couple weeks ago I realized a cool way you can iterate over hashes in ruby. This is probably nothing new for a lot of people, but awesome nonetheless.
For some reason I needed the index, so I’ll show that in my example here.
Okay, so what’s the problem there. Actually, nothing really if that’s the output you want.
I was hoping to be able to iterate over the hash and fetch keys and values direcly,
without doing something like p or p to obtain the key and value. (referencing example above)
Notice the parentheses passed in the block. Obiously, the output difference is clear.
I was curious as to if there were any performance difference between the two. So I wrote a quick benchmark script.
Turns out, virtually no difference in performance. Technically, with parentheses was slightly slower (very slightly). This example is 5,000,000 iterations.
Just thought this was cool, hopefully it helps somone else out.