class Class

def class_attribute(

class_attribute :settings, default: {}

To set a default value for the attribute, pass default:, like so:

To opt out of both instance methods, pass instance_accessor: false.

object.setting = false # => NoMethodError

To opt out of the instance writer method, pass instance_writer: false.

object.setting? # => NoMethodError
object.setting # => NoMethodError

To opt out of the instance reader method, pass instance_reader: false.

Base.setting # => true
object.setting # => false
object.setting = false
object.setting # => true
object = Base.new
Base.setting = true

Instances may overwrite the class value in the same way:

Subclass.setting? # => false

To skip it, pass instance_predicate: false.
For convenience, an instance predicate method is defined as well.

Subclass.setting # => [:foo]
Base.setting # => []
Subclass.setting += [:foo]
Base.setting = []
# Use setters to not propagate changes:

Subclass.setting # => [:foo]
Base.setting # => [:foo]
Subclass.setting << :foo
# Appending in child changes both parent and child because it is the same object:

Subclass.setting # => []
Base.setting # => []
Base.setting = []

In such cases, you don't want to do changes in place. Instead use setters:
when using +class_attribute+ with mutable structures as +Array+ or +Hash+.
on a subclass as overriding the reader method. However, you need to be aware
This matches normal Ruby method inheritance: think of writing an attribute

the value assigned by Subclass would be returned.
would read value assigned to parent class. Once Subclass assigns a value then
by performing Subclass.setting = _something_, Subclass.setting
In the above case as long as Subclass does not assign a value to setting

Base.setting # => true
Subclass.setting # => false
Subclass.setting = false
Subclass.setting # => true
Base.setting = true

end
class Subclass < Base

end
class_attribute :setting
class Base

==== Examples

* :default - Sets a default value for the attribute (defaults to nil).
* :instance_predicate - Sets a predicate method (defaults to true).
* :instance_accessor - Sets both instance methods (defaults to true).
* :instance_writer - Sets the instance writer method (defaults to true).
* :instance_reader - Sets the instance reader method (defaults to true).

==== Options

Subclasses can change their own value and it will not impact parent class.
Declare a class-level attribute whose value is inheritable by subclasses.
def class_attribute(
  *attrs,
  instance_accessor: true,
  instance_reader: instance_accessor,
  instance_writer: instance_accessor,
  instance_predicate: true,
  default: nil
)
  attrs.each do |name|
    singleton_class.silence_redefinition_of_method(name)
    define_singleton_method(name) { default }
    singleton_class.silence_redefinition_of_method("#{name}?")
    define_singleton_method("#{name}?") { !!public_send(name) } if instance_predicate
    ivar = "@#{name}".to_sym
    singleton_class.silence_redefinition_of_method("#{name}=")
    define_singleton_method("#{name}=") do |val|
      redefine_singleton_method(name) { val }
      if singleton_class?
        class_eval do
          redefine_method(name) do
            if instance_variable_defined? ivar
              instance_variable_get ivar
            else
              singleton_class.send name
            end
          end
        end
      end
      val
    end
    if instance_reader
      redefine_method(name) do
        if instance_variable_defined?(ivar)
          instance_variable_get ivar
        else
          self.class.public_send name
        end
      end
      redefine_method("#{name}?") { !!public_send(name) } if instance_predicate
    end
    if instance_writer
      redefine_method("#{name}=") do |val|
        instance_variable_set ivar, val
      end
    end
  end
end

def descendants

C.descendants # => [B, A, D]
class D < C; end

C.descendants # => [B, A]
class A < B; end

C.descendants # => [B]
class B < C; end

C.descendants # => []
class C; end

Returns an array with all classes that are < than its receiver.
def descendants
  descendants = []
  ObjectSpace.each_object(singleton_class) do |k|
    next if k.singleton_class?
    descendants.unshift k unless k == self
  end
  descendants
end

def descendants

JRuby 9.0.4.0 and earlier
def descendants
  descendants = []
  ObjectSpace.each_object(Class) do |k|
    descendants.unshift k if k < self
  end
  descendants.uniq!
  descendants
end

def subclasses

Foo.subclasses # => [Bar]

class Baz < Bar; end
class Bar < Foo; end
class Foo; end

Returns an array with the direct children of +self+.
def subclasses
  subclasses, chain = [], descendants
  chain.each do |k|
    subclasses << k unless chain.any? { |c| c > k }
  end
  subclasses
end