From: Thorsten Hater
Date: 2011-03-04T00:02:02+09:00
Subject: Re: Extracting the shortest string from an array
Hi,
in Ruby 1.9 this should work:
arr.sort_by(&:length)[0]
or more portable/readable (1.8 compatible):
arr.sort_by{|s| s.length }[0]
Am 03.03.2011 15:47, schrieb I単aki Baz Castillo:
> Hi, given the following array:
>
> array = ["qwe", "qwerty"]
>
> How to get the shortest element ("qwe") from the array?
> I whink I must inspect all the elements within the array and manually
> select the shortest string, do I miss some cool feature of Array class
> or Enumerable module?
>
> Thanks a lot.
>
|