← Daily
📝 article syedaslam.com · November 26, 2026

Unpacking arr.max_by &arr.tally

A one-liner from Ruby Weekly #828 returns the most frequent element in an array. This post unpacks why it works : & calls to_proc , and a Hash’s to_proc is a key lookup, which also means it quietly returns nil for missing keys. It also benchmarks the “faster” rewrite, arr.tally.max_by(&:last) : 2.2x faster with many duplicates, 1.4x slower when most elements are unique, with allocation counts showing why.

Read on syedaslam.com →

More from this day

+ Feature your site