When I execute this code:
[[2,"foo"],[1,"bar"]].min{|x|x.first}
I expected [1,"bar"]
as a result, but I get the other entry. Why?
When I execute this code:
[[2,"foo"],[1,"bar"]].min{|x|x.first}
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?
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.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs