Skip to main content

dynamic_constant_definition

🛠️ Dynamic Constant Definition Using const_set and const_get

You can define modules, classes, or any constants at runtime with Module.const_set, which is invaluable for building plugin systems or DSLs. Pair it with Module.const_get for lookup and instantiation without hardcoding names, enabling truly dynamic behavior. Remember to manage the namespace carefully to avoid collisions and consider removing unused constants to prevent memory leaks.

module PluginLoader
def self.register(name, klass)
Plugins.const_set(name, klass)
end

def self.load(name)
klass = Plugins.const_get(name)
klass.new
end
end

module Plugins; end

# Dynamically register a plugin
PluginLoader.register('MyPlugin', Class.new do
def call
puts 'Hello from MyPlugin'
end
end)

# Later, load and use it
PluginLoader.load('MyPlugin').call