From: Josh Cheek Date: 2010-08-03T06:40:53+09:00 Subject: Re: .any?{} Behavior --001636831776e52708048cde0d62 Content-Type: text/plain; charset=ISO-8859-1 What about redefining all? to accept an optional param for what to do when empty? It doesn't change default behaviour, but lets you get your preferred behaviour out of it. (note this only works on 1.9, because of how procs take params in 1.8) module Enumerable old_all = instance_method(:all?) define_method :all? do |emptyval=true,&block| return emptyval if empty? old_all.bind(self).call &block end end require 'test/unit' class NewAllTest < Test::Unit::TestCase def test_empty assert ![].all?(false) assert [].all?(true) assert [].all? end def test_non_empty_no_block assert ![ nil, true, 99 ].all?(true) assert ![ nil, true, 99 ].all?(false) assert ![ nil, true, 99 ].all? assert [ '', true, 99 ].all?(true) assert [ '', true, 99 ].all?(false) assert [ '', true, 99 ].all? end def test_non_empty_block assert [ 1, 3, 99 ].all?( true , &:odd? ) assert [ 1, 3, 99 ].all?( false , &:odd? ) assert [ 1, 3, 99 ].all?( &:odd? ) assert ![ 1, 2, 99 ].all?( true , &:odd? ) assert ![ 1, 2, 99 ].all?( false , &:odd? ) assert ![ 1, 2, 99 ].all?( &:odd? ) end def test_non_array assert Hash[*1..10].all? { |key,value| key.odd? && value.even? } end end --001636831776e52708048cde0d62--