From d5f0d338c7b5d3d64929b51d29061d369550e8c4 Mon Sep 17 00:00:00 2001 From: Marc-Andre Lafortune Date: Tue, 8 Dec 2020 21:20:37 -0500 Subject: Optimize `Enumerable#grep{_v}` [Bug #17030] --- spec/ruby/core/enumerable/grep_spec.rb | 24 +++++++++++++++++------- spec/ruby/core/enumerable/grep_v_spec.rb | 24 +++++++++++++++++------- 2 files changed, 34 insertions(+), 14 deletions(-) (limited to 'spec') diff --git a/spec/ruby/core/enumerable/grep_spec.rb b/spec/ruby/core/enumerable/grep_spec.rb index c9c0f34e27..4e66bb85ea 100644 --- a/spec/ruby/core/enumerable/grep_spec.rb +++ b/spec/ruby/core/enumerable/grep_spec.rb @@ -40,15 +40,25 @@ describe "Enumerable#grep" do $~.should == nil end - it "sets $~ to the last match when given no block" do - "z" =~ /z/ # Reset $~ - ["abc", "def"].grep(/b/).should == ["abc"] + ruby_version_is ""..."3.0.0" do + it "sets $~ to the last match when given no block" do + "z" =~ /z/ # Reset $~ + ["abc", "def"].grep(/b/).should == ["abc"] - # Set by the failed match of "def" - $~.should == nil + # Set by the failed match of "def" + $~.should == nil - ["abc", "def"].grep(/e/) - $&.should == "e" + ["abc", "def"].grep(/e/) + $&.should == "e" + end + end + + ruby_version_is "3.0.0" do + it "does not set $~ when given no block" do + "z" =~ /z/ # Reset $~ + ["abc", "def"].grep(/b/).should == ["abc"] + $&.should == "z" + end end describe "with a block" do diff --git a/spec/ruby/core/enumerable/grep_v_spec.rb b/spec/ruby/core/enumerable/grep_v_spec.rb index 6dec487065..8cae3ac618 100644 --- a/spec/ruby/core/enumerable/grep_v_spec.rb +++ b/spec/ruby/core/enumerable/grep_v_spec.rb @@ -20,15 +20,25 @@ describe "Enumerable#grep_v" do $&.should == "e" end - it "sets $~ to the last match when given no block" do - "z" =~ /z/ # Reset $~ - ["abc", "def"].grep_v(/e/).should == ["abc"] + ruby_version_is ""..."3.0.0" do + it "sets $~ to the last match when given no block" do + "z" =~ /z/ # Reset $~ + ["abc", "def"].grep_v(/e/).should == ["abc"] - # Set by the match of "def" - $&.should == "e" + # Set by the match of "def" + $&.should == "e" - ["abc", "def"].grep_v(/b/) - $&.should == nil + ["abc", "def"].grep_v(/b/) + $&.should == nil + end + end + + ruby_version_is "3.0.0" do + it "does not set $~ when given no block" do + "z" =~ /z/ # Reset $~ + ["abc", "def"].grep_v(/e/).should == ["abc"] + $&.should == "z" + end end describe "without block" do -- cgit v1.2.3