How do I fix this NameError?

Posted by Kyle Kaitan on Stack Overflow See other posts from Stack Overflow or by Kyle Kaitan
Published on 2010-06-15T13:22:11Z Indexed on 2010/06/15 14:02 UTC
Read the original article Hit count: 329

Filed under:
|
|

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?

© Stack Overflow or respective owner

Related posts about ruby

Related posts about scope