From: "Mauricio Fernández" Date: 2003-07-05T15:48:07+09:00 Subject: Re: Ruby Code Snippet: Array.count On Sat, Jul 05, 2003 at 03:43:00PM +0900, Simon Strandgaard wrote: > On Sat, 05 Jul 2003 14:20:35 +0900, Dave Thomas wrote: > > > > class Array > > def count > > inject(0) {|sum, e| yield(e) ? sum + 1 : sum } > > end > > end > > > I have added your Array#count (using inject), here: > > http://www.rubygarden.org/ruby?StandardClassExtensions/Array The other version is faster IMHO, and in this case at least as readable: class Array def count sum = 0 each { |x| sum += 1 if yield x } sum end end -- _ _ | |__ __ _| |_ ___ _ __ ___ __ _ _ __ | '_ \ / _` | __/ __| '_ ` _ \ / _` | '_ \ | |_) | (_| | |_\__ \ | | | | | (_| | | | | |_.__/ \__,_|\__|___/_| |_| |_|\__,_|_| |_| Running Debian GNU/Linux Sid (unstable) batsman dot geo at yahoo dot com 'Ooohh.. "FreeBSD is faster over loopback, when compared to Linux over the wire". Film at 11.' -- Linus Torvalds