diff options
author | Nicholas Seckar <nseckar@gmail.com> | 2005-10-14 03:26:16 +0000 |
---|---|---|
committer | Nicholas Seckar <nseckar@gmail.com> | 2005-10-14 03:26:16 +0000 |
commit | e9df41a75929ad72608eb9a2d3757b200ba1d23d (patch) | |
tree | 1bfd94a2ed8100557723c3912da0e7c0ff61d515 /activesupport | |
parent | 276f14444c2cb840f7076751438dba78ce291a51 (diff) | |
download | rails-e9df41a75929ad72608eb9a2d3757b200ba1d23d.tar.gz rails-e9df41a75929ad72608eb9a2d3757b200ba1d23d.tar.bz2 rails-e9df41a75929ad72608eb9a2d3757b200ba1d23d.zip |
Added Enumerable#first_match
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2578 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activesupport')
-rw-r--r-- | activesupport/CHANGELOG | 2 | ||||
-rw-r--r-- | activesupport/lib/active_support/core_ext/enumerable.rb | 9 | ||||
-rw-r--r-- | activesupport/test/core_ext/enumerable_test.rb | 15 |
3 files changed, 26 insertions, 0 deletions
diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index 12614f3460..486e0ae780 100644 --- a/activesupport/CHANGELOG +++ b/activesupport/CHANGELOG @@ -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. diff --git a/activesupport/lib/active_support/core_ext/enumerable.rb b/activesupport/lib/active_support/core_ext/enumerable.rb new file mode 100644 index 0000000000..497ca52c2d --- /dev/null +++ b/activesupport/lib/active_support/core_ext/enumerable.rb @@ -0,0 +1,9 @@ +module Enumerable + def first_match + match = nil + each do |items| + break if match = yield(items) + end + match + end +end
\ No newline at end of file diff --git a/activesupport/test/core_ext/enumerable_test.rb b/activesupport/test/core_ext/enumerable_test.rb new file mode 100644 index 0000000000..1fcaa9163e --- /dev/null +++ b/activesupport/test/core_ext/enumerable_test.rb @@ -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
\ No newline at end of file |