Suppose I have an enumerated object enum, and now I want to get the third element.
I know that one of the common approaches is to convert to an array and then access with an index, for example:
enum.to_a[2]
But this method will create a temporary array, and it can be inefficient.
Now I use:
enum.each_with_index {|v, i| break v if i == 2}
But this is pretty ugly and redundant.
What is the most efficient way to do this?
source
share