I would even consider it harmful to remember many of these numbers as some sort of practical knowledge. I've actually known cases where a programmer would use some extraneous idioms and when told about a more idiomatic solution it turned out that they knew there was a more idiomatic way of doing it, but they used the more obfuscated alternative because "it was more performant". But it turned out that that knowledge was obsolete (it only applied to an old VM version) or incomplete (it only applied in some very specific cases).
So, beware of "knowing" that `arr.last` is slower than `arr[-1]`. It might not be for too long[2].
[1]: I'm speaking about MRI versions here; of course all those measurements are off if you use JRuby, rbx or Opal.
[2]: It is useful to remember that `arr.bsearch` on sorted arrays is faster than `arr.find`. That probably won't change in the near future ;)
> And some of the other lessons, like "don't use method_missing if you can define a method instead", are well worth considering as well.
I'd argue that this one also falls into the category of "simpler/more readable and, incidentally, more performant".
When defining methods you get the correct behaviour of respond_to? for free, whereas when overriding method_missing, you also have to take care of defining a corresponding respond_to?.
The main reason for choosing def/define_method over method_missing (when possible) should be that it is generally simpler to do so.
Unless it's an execution hotspot in your code, value clarity and maintainability over performance.
Most of the speedups in these optimizations are very small in absolute terms (only a few milliseconds each) so they will only provide a real-world benefit if they're being called in a loop or something.
That all said:
1. A lot of these optimizations are also a win when it comes to clarity (Array#sample is faster and clearer than Array#shuffle.first)
2. Knowing that the "bang" version of a method is always destructive and nearly always faster is a good thing to remember in general for Ruby