5

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?

1 Answer 1

11

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.