module Enumerable def longest_word (strings = map(&:to_s)). zip(strings.map(&:length)). inject([[''],0]) {|(wws, ll), (w, l)| case l <=> ll when -1 then [wws, ll] when 1 then [[w], l] else [wws + [w], ll] end }.first end end
This method depends only on the general Enumerable methods, there is nothing special about Array , so we can pull it into the Enumerable module, where it will also be available for Set or Enumerator s, and not just Array s.
Jörg W Mittag
source share