Added Enumerable#first_match
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2578 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
parent
276f14444c
commit
e9df41a759
@ -1,5 +1,7 @@
|
||||
*SVN*
|
||||
|
||||
* Added Enumerable#first_match [Nicholas Seckar]
|
||||
|
||||
* Fixed that Time#change should also reset usec when also resetting minutes #2459 [ikeda@dream.big.or.jp]
|
||||
|
||||
* Fix Logger compatibility for distributions that don't keep Ruby and its standard library in sync.
|
||||
|
9
activesupport/lib/active_support/core_ext/enumerable.rb
Normal file
9
activesupport/lib/active_support/core_ext/enumerable.rb
Normal file
@ -0,0 +1,9 @@
|
||||
module Enumerable
|
||||
def first_match
|
||||
match = nil
|
||||
each do |items|
|
||||
break if match = yield(items)
|
||||
end
|
||||
match
|
||||
end
|
||||
end
|
15
activesupport/test/core_ext/enumerable_test.rb
Normal file
15
activesupport/test/core_ext/enumerable_test.rb
Normal file
@ -0,0 +1,15 @@
|
||||
require 'test/unit'
|
||||
require File.dirname(__FILE__) + '/../../lib/active_support/core_ext/enumerable'
|
||||
|
||||
class EnumerableTests < Test::Unit::TestCase
|
||||
|
||||
def test_first_match_no_match
|
||||
[[1, 2, 3, 4, 5], (1..9)].each {|a| a.first_match {|x| x > 10}}
|
||||
end
|
||||
|
||||
def test_first_match_with_match
|
||||
assert_equal true, [1, 2, 3, 4, 5, 6].first_match {|x| x > 4}
|
||||
assert_equal true, (1..10).first_match {|x| x > 9}
|
||||
assert_equal :aba, {:a => 10, :aba => 50, :bac => 40}.first_match {|k, v| k if v > 45}
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue
Block a user