1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
module Enumerable
  def mdetect
    each { |o| v = yield(o); return v if v }
    return nil
  end
end

describe "mdetect" do
  it "should return the value returned from the block" do
    ['cat food', 'dog food', 'bird food'].mdetect { |s| s.match(/dog/) }.should be_an_instance_of(MatchData)
  end
  
  it "should return nil if nothing is found" do
    [1, 2, 3].mdetect { false }.should be_nil
  end
end