From: "mame (Yusuke Endoh)" <noreply@...>
Date: 2021-11-16T07:41:38+00:00
Subject: [ruby-core:106079] [Ruby master Feature#18273] Class#subclasses

Issue #18273 has been updated by mame (Yusuke Endoh).


I am afraid if the name "subclasses" is suitable. When we have three classes `C`, `D` that inherits from `C`, and `E` that inherits from `D`, we say "E is a subclass of C".

In fact, the rdoc of `Module#<` says

> mod < other ��� true, false, or nil
>
> Returns true if mod is a subclass of other.

and `E < C` returns true.

```
class C; end
class D < C; end
class E < D; end

p E < C #=> true
```

So, `Class#subclasses` sounds the same as `Class#descendants`.

If we take family line as an analogy, `Class#children` might be good. @ko1 suggested `Class#direct_subclasses`.

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

* 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>