Project

General

Profile

Feature #15327

Proposal: Enable refinements to `#respond_to?`

Added by osyo (manga osyo) 22 days ago. Updated 21 days ago.

Status:
Closed
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:89922]

Description

Refinements are enabled with various contexts and methods.

  • #send
  • #__send__
  • Symbol#to_proc
  • #to_s
    • called by "#{hoge}"
  • #to_proc
    • call to &:hoge
    • since ruby 2.6

I want to enable refinements for #respond_to?
I want to use it in the following case.

module StringEx
    refine String do
        def twice
            self + self
        end
    end
end

class X
    using StringEx

    def meth a
        a.twice if a.respond_to? :twice
    end
end

class Y
    def twice
        42 + 42
    end
end

x = X.new

p x.meth Y.new
# => 84

p x.meth "homu"
# => "homuhomu"

p x.meth 42
# nil

pull request : https://github.com/ruby/ruby/pull/2020

Associated revisions

Revision 16a642c3
Added by nobu (Nobuyoshi Nakada) 21 days ago

Enable refinements at Object#respond_to?

[Feature #15327] [Fix GH-2020]

From: osyo-manga manga.osyo@gmail.com

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65920 b2dd03c8-39d4-4d8f-98ff-823fe69b080e

Revision 65920
Added by nobu (Nobuyoshi Nakada) 21 days ago

Enable refinements at Object#respond_to?

[Feature #15327] [Fix GH-2020]

From: osyo-manga manga.osyo@gmail.com

History

#1 [ruby-core:89951] Updated by nobu (Nobuyoshi Nakada) 21 days ago

defined?(obj.method) should work.

#2 Updated by nobu (Nobuyoshi Nakada) 21 days ago

  • Status changed from Open to Closed

Applied in changeset trunk|r65920.


Enable refinements at Object#respond_to?

[Feature #15327] [Fix GH-2020]

From: osyo-manga manga.osyo@gmail.com

Also available in: Atom PDF