From: Stefan Arentz Date: 2003-07-05T08:01:38+09:00 Subject: Ruby Code Snippet: Array.count Maybe this functionality is already somewhere in Ruby, but I could not find it. This little snippet extends Array with a count method. It takes a code block that can be used to count elements that match a specific condition: a = Array[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] puts a.count { |v| v < 5 } ==> 4 The code: class Array def count(&action) count = 0 self.each { |v| count = count + 1 if action.call(v) } return count end end Ruby is sweet :-) S. -- stefan.arentz@soze.com / http://www.soze.com/stefan "Think of Your cellular phone as a party line", Ian Goldberg