If you want to collect the first n objects that satisfy a condition then maybe use each_with_object or with_object with an array object and build it up until you collect n objects.
The thing: I want elements that are highest among all the other elements, not the ones that meet a specific condition. Would you please provide an example of how to accomplish it using each_with_object or with_object?
Then I would not recommend each_with_object or with_object. On another note be aware of behaviour when using max_by with duplicate elements i.e. [9,9,8].max_by(2, &:itself) #=> [9, 9]. And also note there is the corresponding min_by method too.
arr.max_by(n) {|i| #condition }in certain cases will work.nobjects that satisfy a condition then maybe useeach_with_objectorwith_objectwith an array object and build it up until you collectnobjects.each_with_objectorwith_object?each_with_objectorwith_object. On another note be aware of behaviour when usingmax_bywith duplicate elements i.e.[9,9,8].max_by(2, &:itself) #=> [9, 9]. And also note there is the correspondingmin_bymethod too.