Skip to main content

singleton_class_hierarchy

🛠️ Navigating and Altering Singleton Class Hierarchies

Every Ruby object has a singleton class for per‑object behavior. You can insert modules into its ancestor chain, override methods on the fly, or introspect the exact method lookup path.

obj = "hello"
# Inspect original ancestors
p obj.singleton_class.ancestors
# Dynamically prepend a module
mod = Module.new do
def upcase
"INTERCEPT: #{super}"
end
end
obj.singleton_class.prepend(mod)
p obj.upcase # => "INTERCEPT: HELLO"
# Remove the module later
obj.singleton_class.send(:undef_method, :upcase)

Use singleton_class.ancestors, prepend, and remove_method to surgically inject or retract per‑object behavior without touching global classes.