r/programming • • Dec 25 '16

Ruby 2.4.0 Released

https://www.ruby-lang.org/en/news/2016/12/25/ruby-2-4-0-released/
56 Upvotes

9 comments sorted by

View all comments

5

u/NeuroXc Dec 25 '16

Wait a second...

[x, y].max and [x, y].min are optimized to not create a temporary array under certain conditions.

Why would you need to ever create a temporary array to find the max or min in an array?

17

u/Hueho Dec 25 '16

The devil is in details. And also in unclear explanations!

Normal #min and #max methods don't allocate other arrays, the problem is that is idiomatic in Ruby to calculate the max/min of 2 variables by wrapping them in a array literal and calling the respective method, instead of doing a if or ternary. This of course has much bigger overhead, including an useless array allocation.

The change is essentially just rewriting these occurrences in the bytecode compiler to the simple version.