Project

General

Profile

Actions

Bug #9581

closed

`=~` defined on a subclass of `String` is sometimes ignored, and `String#=~` is called instead

Added by sawa (Tsuyoshi Sawada) over 10 years ago. Updated over 10 years ago.

Status:
Closed
Assignee:
-
Target version:
-
[ruby-core:61151]

Description

As is reported on StackOverflow (http://stackoverflow.com/questions/22103018) by Gabriel, overridden =~ on a subclass of String is sometimes ignored, and the original String#=~ is called. Particularly, when we have:

class MyString < String
  def =~ re; :foo end
end
s = MyString.new("abc")

these give the correct result:

r = /abc/; s =~ r   # => :foo
s.send(:=~, r)      # => :foo
s.send(:=~, /abc/)  # => :foo

but in this case, MyString#=~ is ignored, and String#=~ is called instead:

s =~ /abc/          # => 0
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0