DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Finding The Length Of The Longest Word

04.28.2010
| 3255 views |
  • submit to reddit
        Source: <a href="http://ruby-doc.org/core/classes/Enumerable.html#M003140">Module: Enumerable</a> [ruby-doc.org]

<snip>
longest = %w{ cat sheep bear }.inject(0) do |memo,word|
  memo >= word.length ? memo : word.length
end
longest                                         #=> 5
</snip>