After using the keyword def
, a new scope is created, so the issue here is that the const_name
variable is no longer in scope inside the body of the method_missing
method.
You can keep the variable in scope by using blocks like so:
def self.const_missing(const_name)
anon_class = Class.new do
define_singleton_method(:method_missing) do |method_name, *params|
params = {
'Target' => const_name.to_s,
'Method' => method_name.to_s,
}
APIWrapper.call_get params
end
end
end
You might want to also set the constant to the anonymous class you just created:
anon_class = Class.new do
...
end
const_set const_name, anon_class