Project

General

Profile

Feature #15373

Proposal: Enable refinements to `#method` and `#instance_method`

Added by osyo (manga osyo) 10 days ago. Updated 10 days ago.

Status:
Open
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:90253]

Description

Proposal enable refinements to #method and #instance_method.
It can be used in the following cases.

# default call to #pp
module P2PP
  refine Kernel do
    def p obj
      pp obj
    end
  end
end

using P2PP

# call to Kernel.#pp
[1, 2, 3].map(&method(:p))

Also, when checking the defined position as follows, the refine place is reflected.

module P2PP
  refine Kernel do
    def p obj
      pp obj
    end
  end
end

# Output to refine defined position
# Before
p method(:p).source_location
# => nil
p Kernel.instance_method(:p).source_location
# => nil

using P2PP

# After
p method(:p).source_location
# => ["../ruby/test.rb", 4]
p Kernel.instance_method(:p).source_location
# => ["../ruby/test.rb", 4]

I want .: operators to work.
https://bugs.ruby-lang.org/issues/13581

pull reqiest: https://github.com/ruby/ruby/pull/2034

History

#1 [ruby-core:90254] Updated by shevegen (Robert A. Heiler) 10 days ago

I won't comment on the .: here and not about the first part
of the suggestions here either, but about .source_location
I agree as that would make sense (to me at the least).

Returning something that exists is better than returning
nil in this context. In a REPL this can be useful too since
one can e. g. automatically "jump" to the code in question
and modify it (I think pry is doing something like this).

I think matz has to decide whether he wants that functionality
to work (in regards to .source_location; I don't refer to
the other parts of the issue).

If approved I would then recommend, if it is changed, to
also add a comment for the documentation of
.source_location to mention that it works with refinements
too, and perhaps add an example, similar to the one
in this issue request:

https://ruby-doc.org/core/Method.html#method-i-source_location

#2 [ruby-core:90259] Updated by osyo (manga osyo) 10 days ago

Thank you! shevegen :)

Also available in: Atom PDF