basics/not_match_operator_no_parens.rb

tests/_examples/basics/not_match_operator_no_parens.rb in the metorex repository.

puts ("hello" !~ /ell/).inspect
puts ("hello" !~ /xyz/).inspect
puts (/ell/ !~ "hello").inspect

class Matcher
  def =~ other
    other == :expected
  end
end

matcher = Matcher.new
puts (matcher !~ :expected).inspect
puts (matcher !~ :other).inspect

class Overridden
  def !~ other
    :custom
  end
end

overridden = Overridden.new !~ :anything
puts overridden.inspect

begin
  Object.new !~ :foo
rescue NoMethodError => error
  puts "#{error.class}: #{error.message}"
end

begin
  42 !~ 99
rescue NoMethodError => error
  puts error.message
end

Output:

false
true
false
false
true
:custom
NoMethodError: undefined method '=~' for an instance of Object
undefined method '=~' for an instance of Integer