How do I fix this NameError?
- by Kyle Kaitan
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?