"min" doesn't work

When I execute this code:


I expected [1,"bar"] as a result, but I get the other entry. Why?

I found a solution, problem was that it expects a code block with 2 parameters. So this works: min{|x,y|x.first<=>y.first}. But is there a simpler or better way?

There’s min_by:

[[2,"foo"],[1,"bar"]].min_by &:first
1 Like

Correct. You were making the mistake of using a max_by block with max, which takes a comparator instead. Either way, glad you figured it out.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs