[‘fodfsafdfsdfso’,‘bar’,‘bazzzzzz’].sort_by { |s| s.length }.pop
The beauty of a one line solution
also…what is this about:
max_by(&:length)
Dave.
[‘fodfsafdfsdfso’,‘bar’,‘bazzzzzz’].sort_by { |s| s.length }.pop
The beauty of a one line solution
also…what is this about:
max_by(&:length)
Dave.
Daniel M. wrote:
Why build up a separate hash structure?
It’s simpler to my eyes. It’s also (admittedly unintuitively) faster
though.
Here’s a quick test with your version, my original, and another going
off your idea, but I tried to simplify it a bit:
bm.report { puts a.max_by3 &:size }
end
I couldn’t tell you why the 3rd is so much faster than the first. Just
goes to prove the old programming adage though: “Optimizing before
Profiling is a waste of time.”
Not that this counts as serious profiling of course.
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