Issue #18273 has been updated by sawa (Tsuyoshi Sawada).


mame (Yusuke Endoh) wrote in #note-8:
> I think we don't say "C is a superclass of E" in the same situation, but I'm unsure.

If there is any sense in understanding the word "superclass" to mean only the direct ancestor, that is because it is in singular form, not because the word per se implies directness. The singular form leads a programmer to infer that it must mean something unique, hence the direct ancestor, excluding the indirect ones. The word itself does not imply directness. In fact, there are some definitions out there that explicitly mention indirect ancestors to be included in "superclass": https://www.whitman.edu/mathematics/java_tutorial/java/javaOO/subclasses.html

> The term superclass refers to a class's direct ancestor as well as all of its ascendant classes.

When it comes to subclass, even the direct ones are not unique, which means that directness cannot be distinguished by referring to singularity, and it is this fact that is making it clearer that the word does not imply directness.

So my suggestion is that, as pointed out by mame, avoid the method name "subclasses", and deprecate "superclass" in favor of something else.

From compatibility and uniformity with other methods involving (in)directness such as `Module#instance_methods`, I now think it is a good idea to make the directness parameter to be positional, with truthy value meaning "including indirect", and falsy meaning "only direct", default being truthy.

----------------------------------------
Feature #18273: Class#subclasses
https://bugs.ruby-lang.org/issues/18273#change-94691

* Author: byroot (Jean Boussier)
* Status: Open
* Priority: Normal
----------------------------------------
Ref: https://github.com/rails/rails/pull/43481

Something we forgot to mention in [Feature #14394], is either a parameter or another method to only get direct descendants.

Active Support has been offering `Class.subclasses` as:

```ruby
  def subclasses
    descendants.select { |descendant| descendant.superclass == self }
  end
```

It seems a bit silly to grab all descendants and then restrict the list when `Class#descendants` had to do some recursion to get them all in the first place.

### Proposal

We could either implement `Class#subclasses` directly, or accept a parameter in `Class#descendants`, e.g. `descendants(immediate = false)`.

cc @eregon



-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>