在阅读了对另一个问题的答案的评论并进行了一些研究后,我发现它=~是定义的Object,然后被Stringand覆盖Regexp。String和Regexp似乎假设另一个类的实现:
"123" =~ "123" # => TypeError: type mismatch: String given
/123/ =~ /123/ # => TypeError: can't convert Regexp to String
虽然=~是为 定义的Object,+但不是:
Object.new =~ 1 # => nil
Object.new + 1 # => undefined method `+' for #<Object:0x556d38>
为什么Object#=~已经定义,而不是限制=~到String和Regexp?