Assume I have an enumerable object enum and now I want to get the third item.
I know one of a general approach is convert into an array and then access with index like:
enum.to_a[2]
But this way will create a temporary array and it might be inefficient.
Now I use:
enum.each_with_index {|v, i| break v if i == 2}
But this is quite ugly and redundant.
What’s the most efficient way to do this?
You could use
taketo peel off the first three elements and thenlastto grab the third element from the array thattakegives you:If you don’t want to generate any arrays at all then perhaps: