I want to use the value v inside of an instance method on the metaclass of a particular object:
v = ParserMap[kind][:validation] # We want to use this value later.
s = ParserMap[kind][:specs]
const_set(name, lambda {
p = Parser.new(&s)
# This line starts a new scope...
class << p
define_method :validate do |opts|
v.call(self, opts) # => NameError! The `class` keyword above
# has started a new scope and we lost
# old `v`.
end
end
p
})
Unfortunately, the class keyword starts a new scope, so I lose the old scope and I get a NameError. How do I fix this?
Replace
class << pwithclass << p; self end.class_eval doand it will work.class << p; self endwill return the metaclass of p, so you can callclass_evalon it. The block given toclass_evalwill then execute in the context of the metaclass (same as it did before), but without starting a new scope.