Project

General

Profile

Actions

Feature #17749

open

Const source location without name

Added by tenderlovemaking (Aaron Patterson) over 3 years ago. Updated over 3 years ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:103026]

Description

Hi,

I would like to be able to ask a class or module what its source location is without knowing the name. For example, I want to do this:

module A
  class B
  end
end

p A::B.const_source_location

In other works A::B.const_source_location would be equivalent to A.const_source_location(:B).

The reason I want to do this is because sometimes it is very difficult to get the name of a constant, and sometimes I don't have access to the constant that "encloses" the class or module.

One example:

ObjectSpace.each_object(Class) do |k|
  p k.const_source_location
end

In this case I have class objects, but I can't tell what constant k was defined inside of. Also I can't trust the "name" method on k because sometimes it's not the default method (of course I could work around that, but it's not fun).

I've attached a patch that implements the feature, and there is a PR here

Side note: I think I would like "source_location" better than const_source_location, but I wanted to just file a feature request so we could talk about the feature in general.


Files


Related issues 1 (0 open1 closed)

Related to Ruby master - Feature #13383: [PATCH] Module#source_locationRejectedioquatix (Samuel Williams)Actions
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0