From: shevegen@... Date: 2021-03-26T02:01:50+00:00 Subject: [ruby-core:103028] [Ruby master Feature#17749] Const source location without name Issue #17749 has been updated by shevegen (Robert A. Heiler). Personally I agree with Aaron's use case or the general potential usefulness of being able to query const_source_location where Aaron's suggestion is a simplification over A::B (because the ruby user no longer HAS to know that specific name, since the ruby user can just call a method instead, in this case simply **.const_source_location()** ); and I love introspection anyway, so +1. To the comment about the **name** (s): .source_location is in general better than .const_source_location, in my opinion, for **two reasons** : (1) it's shorter. :) We all like being concise ... two words are better than three words most of the time. (One word may be even better but we often have clashes with other names/methods, so two words are a bit better in these cases.) (2) even aside from (1), I believe .source_location is better as a name from a design point of view. While Dylan reasoned that **.const_source_location()** makes more sense, IMO we could actually go the other approach and say "ruby, I don't quite care if it is a constant, or a method, or a class, or anything, I just want to query the source_location to it". So I think it depends on the point of view you have for the name. In my opinion, omitting "const_" would be perfectly fine; also I struggle with things such as "when to use class_eval or instance_eval" so I am all for simpler names in general. ;) Now we do have Method#source_location but I think and also const_source_location as well - but I think it would be simpler to "unify" towards one general name, no matter what that name may be, if we look at it from the point of view of "how ruby users may want to use the feature". So from that point of view, from a ruby user perspective, I think it would be better to omit "const_" completely. But this is quite secondary; IMO the use case described makes sense. ---------------------------------------- Feature #17749: Const source location without name https://bugs.ruby-lang.org/issues/17749#change-91092 * Author: tenderlovemaking (Aaron Patterson) * Status: Open * Priority: Normal ---------------------------------------- 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: ```ruby 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: ```ruby 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](https://github.com/ruby/ruby/pull/4324) 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-------------------------------- 0001-Add-constant-location-information-to-classes.patch (5.34 KB) -- https://bugs.ruby-lang.org/ Unsubscribe: