From 56d0e1a52ca7efb2ad427c495a98e13e638493ac Mon Sep 17 00:00:00 2001 From: Damian Janowski Date: Thu, 19 Jun 2008 17:07:33 -0300 Subject: [PATCH] Allowing Enumerable#many? to receive a block. --- .../lib/active_support/core_ext/enumerable.rb | 3 ++- activesupport/test/core_ext/enumerable_test.rb | 9 +++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/activesupport/lib/active_support/core_ext/enumerable.rb b/activesupport/lib/active_support/core_ext/enumerable.rb index 9647797..104277d 100644 --- a/activesupport/lib/active_support/core_ext/enumerable.rb +++ b/activesupport/lib/active_support/core_ext/enumerable.rb @@ -79,7 +79,8 @@ module Enumerable end # Returns true if the collection has more than 1 element. Functionally equivalent to collection.size > 1. - def many? + def many?(&block) + size = block_given?? select(&block).size : self.size size > 1 end end diff --git a/activesupport/test/core_ext/enumerable_test.rb b/activesupport/test/core_ext/enumerable_test.rb index fb9b9b8..2315d8f 100644 --- a/activesupport/test/core_ext/enumerable_test.rb +++ b/activesupport/test/core_ext/enumerable_test.rb @@ -63,10 +63,15 @@ class EnumerableTests < Test::Unit::TestCase assert_equal({ 5 => payments[0], 15 => payments[1], 10 => payments[2] }, payments.index_by { |p| p.price }) end - - def test_several + + def test_many assert ![].many? assert ![ 1 ].many? assert [ 1, 2 ].many? + + assert ![].many? {|x| x > 1 } + assert ![ 2 ].many? {|x| x > 1 } + assert ![ 1, 2 ].many? {|x| x > 1 } + assert [ 1, 2, 2 ].many? {|x| x > 1 } end end -- 1.5.5.1