From: Josh Cheek Date: 2011-03-04T03:18:12+09:00 Subject: Re: Extracting the shortest string from an array --000e0ce0f316260cf8049d980bf2 Content-Type: text/plain; charset=ISO-8859-1 On Thu, Mar 3, 2011 at 11:35 AM, Robert Klemme wrote: > On 03.03.2011 16:02, Thorsten Hater wrote: > >> in Ruby 1.9 this should work: >> >> arr.sort_by(&:length)[0] >> > > Inefficient as it creates a new array. Better do > > irb(main):001:0> ["qwe", "qwerty"].min_by(&:length) > => "qwe" > > > or more portable/readable (1.8 compatible): >> >> arr.sort_by{|s| s.length }[0] >> > > Inefficient as well (see above). > > Cheers > > robert > > -- > remember.guy do |as, often| as.you_can - without end > http://blog.rubybestpractices.com/ > > They're also O( n lg n ) where the correct solution, using min, is O(n) --000e0ce0f316260cf8049d980bf2--