Issue #8053 has been reported by prijutme4ty (Ilya Vorontsov).

----------------------------------------
Feature #8053: Make coercion if #=== operator doesn't know what to do
https://bugs.ruby-lang.org/issues/8053

Author: prijutme4ty (Ilya Vorontsov)
Status: Open
Priority: Normal
Assignee: 
Category: 
Target version: 


Related to http://bugs.ruby-lang.org/issues/7604
It'd be fine if === will do coerce when it doesn't know what to do. In ticket above I gave use-case for case-statement which needs that any ruby object could be coerced to a "pattern".
For example

case arr.end_with?
when ['several', 'words'] then puts 'arr have last two elements: several, words'
when ['word'] then puts 'arr have last element: word'
else puts 'cannot understand'
end

It'd be possible if Array#===(predicate) will simply use usual coerce call: predicate.coerce(arr) -- Predicate#coerce is a good point to understand behavior of such matching.

Another example:

class FalseYielder
  def ===(other)
    false
  end
end
class MyString
  def initalize(string); @string = string; end
  attr_reader :string
  def coerce(other)
    if other.is_a? Regexp
      [other, self.string]
    else
      [FalseYielder.new, self]
    end
  end
end

case MyString.new('abcpatdef')
when /pat/ then 'it works'
end

Here we shouldn't monkeypatch Regexp class and it's good, we can make an error only in our own class MyString.


-- 
http://bugs.ruby-lang.org/