Project

General

Profile

Actions

Feature #17674

closed

Proposal: `Method#source_location` or `Method#owner` for refined methods

Added by AlexWayfer (Alexander Popov) about 3 years ago. Updated about 3 years ago.

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

Description

Inspired by https://bugs.ruby-lang.org/issues/15504#note-17

I'm working on a gem with Ruby refinements, and some methods are invented in new Ruby versions like 2.5 or 3.0, and I'd like to define in this gem should I refine a method or not.

So… current behavior of Method#source_location returns nil for core and/or refined methods. I think, refinements should be caught by this method. Also I didn't know about Method#owner, but it can be a case for refinements too.

Updated by osyo (manga osyo) about 3 years ago

hi.
In Ruby 2.7 and later, #method supports Refinements.

# test.rb
module Cover
  refine Range do
    def cover?(value)
      return super unless value.is_a?(Range)

      super(value.first) && super(value.last)
    end
  end
end
using Cover

pp (1..10).method(:cover?).source_location
# Ruby 2.6 => nil
# Ruby 2.7 => ["/test.rb", 4]

pp (1..10).method(:cover?).owner
# Ruby 2.6 => Range
# Ruby 2.7 => #<refinement:Range@Cover>

see: https://bugs.ruby-lang.org/issues/15373

Updated by AlexWayfer (Alexander Popov) about 3 years ago

osyo (manga osyo) wrote in #note-1:

hi.
In Ruby 2.7 and later, #method supports Refinements.

# test.rb
module Cover
  refine Range do
    def cover?(value)
      return super unless value.is_a?(Range)

      super(value.first) && super(value.last)
    end
  end
end
using Cover

pp (1..10).method(:cover?).source_location
# Ruby 2.6 => nil
# Ruby 2.7 => ["/test.rb", 4]

pp (1..10).method(:cover?).owner
# Ruby 2.6 => Range
# Ruby 2.7 => #<refinement:Range@Cover>

see: https://bugs.ruby-lang.org/issues/15373

Can't confirm:

# frozen_string_literal: true

puts RUBY_VERSION
p (1..10).method(:cover?).source_location
~/Projects/ruby/test/source_location
> ruby test.rb
3.0.0
nil

~/Projects/ruby/test/source_location
> rbenv local 2.7.2

~/Projects/ruby/test/source_location
> ruby test.rb
2.7.2
nil

I'd like to attach a link to something like repl.it, but there is only Ruby 2.5.

Updated by xtkoba (Tee KOBAYASHI) about 3 years ago

The example code in #note-2 does not seem to refine Range#cover?. This method is defined natively in range.c, and so the behavior is expected (Method#source_location should return nil).

Updated by AlexWayfer (Alexander Popov) about 3 years ago

xtkoba (Tee KOBAYASHI) wrote in #note-3:

The example code in #note-2 does not seem to refine Range#cover?. This method is defined natively in range.c, and so the behavior is expected (Method#source_location should return nil).

Sorry, you're correct, I didn't understand the code from #note-1 correctly.

(I don't see an ability to close this issue by myself)

Actions #5

Updated by jeremyevans0 (Jeremy Evans) about 3 years ago

  • Status changed from Open to Closed
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0