3 ms·
There's #lazy to turn things into a lazy enumerator, to be iterated over when you so desire with e.g #force. If you're going to iterate over an accumulator var
by lloeki 3y ago
There's #lazy to turn things into a lazy enumerator, to be iterated over when you so desire with e.g #force.
If you're going to iterate over an accumulator variable, use the for keyword instead of each, it's faster.
Alternatively, one can use .reduce({}) { |h, (k, v)| ... h } or .each.with_object({}) { |(k, v), h| ... } which makes the block not close over an external variable, and makes the assignment to that variable "atomic" (wrt the hash construction, the variable will only contain the final result, that is if a final variable is needed at all, which it may not with implicit return of the last value)