views:

243

answers:

1

I was looking at the Ruby logging library Logging.logger method and have a question from the source at github relating to this piece of code:

  logger = ::Logging::Logger.new(name)
  logger.add_appenders appender
  logger.additive = false

  class << logger
    def close
      @appenders.each {|a| a.close}
      h = ::Logging::Repository.instance.instance_variable_get :@h
      h.delete(@name)
      class << self; undef :close; end
    end
  end

I understand that the class << logger opens up the eigen/meta/singleton class to the logger object to add an instance specifice close method. However, I am not exactly sure what the "class << self; undef :close; end" does and for what purpose. Can anyone tell me what it means?

+6  A: 

this actually deletes the method (when it actually gets executed). It's a safeguard to make sure close is not called twice. It kind of looks like there are nested 'class << ' constructs, but there aren't. The inner class << is executed when the method is called and the outer class << is called when the method is defined.

David Nehme