Skip to content

Commit 89e9edc

Browse files
committed
Add specs for $~ in Enumerable#{grep,grep_v}
1 parent 2b1b37a commit 89e9edc

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

spec/ruby/core/enumerable/grep_spec.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,28 @@ class EnumerableSpecGrep2; def ===(obj); /^ca/ =~ obj; end; end
2929
ary.grep(/a(b)a/) { $1 }.should == ["b", "b"]
3030
end
3131

32+
it "sets $~ in the block" do
33+
"z" =~ /z/ # Reset $~
34+
["abc", "def"].grep(/b/) { |e|
35+
e.should == "abc"
36+
$&.should == "b"
37+
}
38+
39+
# Set by the failed match of "def"
40+
$~.should == nil
41+
end
42+
43+
it "sets $~ to the last match when given no block" do
44+
"z" =~ /z/ # Reset $~
45+
["abc", "def"].grep(/b/).should == ["abc"]
46+
47+
# Set by the failed match of "def"
48+
$~.should == nil
49+
50+
["abc", "def"].grep(/e/)
51+
$&.should == "e"
52+
end
53+
3254
describe "with a block" do
3355
before :each do
3456
@numerous = EnumerableSpecs::Numerous.new(*(0..9).to_a)

spec/ruby/core/enumerable/grep_v_spec.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,28 @@ def (@odd_matcher = BasicObject.new).===(obj)
99
end
1010
end
1111

12+
it "sets $~ in the block" do
13+
"z" =~ /z/ # Reset $~
14+
["abc", "def"].grep_v(/e/) { |e|
15+
e.should == "abc"
16+
$~.should == nil
17+
}
18+
19+
# Set by the match of "def"
20+
$&.should == "e"
21+
end
22+
23+
it "sets $~ to the last match when given no block" do
24+
"z" =~ /z/ # Reset $~
25+
["abc", "def"].grep_v(/e/).should == ["abc"]
26+
27+
# Set by the match of "def"
28+
$&.should == "e"
29+
30+
["abc", "def"].grep_v(/b/)
31+
$&.should == nil
32+
end
33+
1234
describe "without block" do
1335
it "returns an Array of matched elements" do
1436
@numerous.grep_v(@odd_matcher).should == [0, 2, 4, 6, 8]

0 commit comments

Comments
 (0)