Skip to content

Commit 5b848b1

Browse files
committed
Fix Kernel#!~ and raise NoMethodError when #~ isn't implemented
1 parent 18bb410 commit 5b848b1

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

spec/ruby/core/kernel/not_match_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,20 @@ def !~(obj)
1414
(obj !~ :foo).should == false
1515
end
1616

17+
ruby_version_is ""..."3.2" do
18+
it "returns true if self does not respond to #=~" do
19+
suppress_warning do
20+
(Object.new !~ :foo).should == true
21+
end
22+
end
23+
end
24+
25+
ruby_version_is "3.2" do
26+
it "raises NoMethodError if self does not respond to #=~" do
27+
-> { Object.new !~ :foo }.should raise_error(NoMethodError)
28+
end
29+
end
30+
1731
it 'can be overridden in subclasses' do
1832
obj = KernelSpecs::NotMatch.new
1933
(obj !~ :bar).should == :foo

src/main/ruby/truffleruby/core/kernel.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ def `(str) #`
182182
module_function :` # `
183183

184184
def !~(other)
185-
r = self.respond_to?(:=~) ? !(self =~ other) : true
185+
r = self =~ other ? false : true
186186
Primitive.regexp_last_match_set(Primitive.caller_special_variables, $~)
187187
r
188188
end

0 commit comments

Comments
 (0)