Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
class method
Module.nesting
() -> ::Array[::Module]
Module.nesting → array
eval.c
View on GitHub ↗
static VALUE
rb_mod_nesting(VALUE _)
{
VALUE ary = rb_ary_new();
const rb_cref_t *cref = rb_vm_cref();
while (cref && CREF_NEXT(cref)) {
VALUE klass = CREF_CLASS(cref);
if (!CREF_PUSHED_BY_EVAL(cref) &&
!NIL_P(klass)) {
rb_ary_push(ary, klass);
}
cref = CREF_NEXT(cref);
}
return ary;
}
Returns nested module as an array of Module objects:
Example
module M0 def self.speak = Module.nesting module M1 def self.speak = Module.nesting module M2 def self.speak = Module.nesting end end end M0.speak # => [M0] M0.speak.first.class # => Module M0::M1.speak # => [M0::M1, M0] M0::M1::M2.speak # => [M0::M1::M2, M0::M1, M0]