Tbpgr Blog

Employee Experience Engineer tbpgr(てぃーびー) のブログ

ActiveSupport | Class#descendants

概要

Class#descendants

詳細

Class#descendants について

Class#descendants

子孫クラスを取得する。descendants=子孫
サブクラス、サブクラスのサブクラス等特定クラスから継承された全てのクラスを取得する。

サンプル

# encoding: utf-8
require 'active_support/core_ext/class/subclasses'
require 'tbpgr_utils'

class A;end
 
class B < A;end
class D < A;end
 
class C < B; end

bulk_puts_eval binding, <<-EOS
A.descendants
B.descendants
C.descendants
EOS

__END__
下記はTbpgrUtils gemの機能

bulk_puts_eval

詳しくは下記参照
https://rubygems.org/gems/tbpgr_utils
https://github.com/tbpgr/tbpgr_utils

出力

A.descendants # => [B, D, C]
B.descendants # => [C]
C.descendants # => []