You think about it wrong. There is no such thing as nesting methods. Constants are nested somewhere. Nesting has path resolution associated with module and class names. Methods are contained in a module / class (whether it is "normal" or single-line).
If the method is placed in semantics. There is a concept similar to self
that defines where a method called default definee will be defined. There is no keyword for this, but it is roughly equivalent:
kind_of?(Module) ? name : self.class.name
If a constant is placed / searched, it is purely syntactic. When you reference X
, it doesnβt care if you put it in a method or not:
DEEP_MIND = Object.new module Foo X = 42 end module Foo module Bar def DEEP_MIND.talk p X end end end DEEP_MIND.talk
All he cares about is that the "current nesting" in the line of code is where you tried to reference it.
Now, if you really wanted to find the "current nest inside the method body", you need to somehow pretend that you are actually there.
Unfortunately, I donβt think there is another way than the one shown in @ Eric answer . Using a block with instance_eval
/ instance_exec
will give you a nesting of where the block was defined.
source share