1

我的期望在评论中。

require 'logger'

module Logging
  attr_accessor :logger

  def logger
    return @logger if @logger # allow items to have own loggers
    @@logger ||= Logger.new(STDERR)
    puts "Instance Class REF ID#{@@logger.__id__}"
    puts "Class ID #{self.class.logger.__id__}"
    @@logger
  end

  module ClassMethods
    def logger= logger
      @logger = logger
    end

    def logger
      @logger ||= Logger.new(STDERR)
      puts "Class Instance REF ID #{@logger.__id__}"
      @logger
    end
  end

  def self.included(base)
    base.extend(ClassMethods)
  end
end

class Test

  include Logging

  def wow
    logger.info 'wow'
  end
end

t = Test.new

# should be the same
puts "Loggers are #{t.logger.__id__ == Test.logger.__id__ ? '' : 'not '}the same"

Test.logger = Logger.new('/dev/null')

# should still be the same
puts "Loggers are #{t.logger.__id__ == Test.logger.__id__ ? '' : 'not '}the same"

lg = Test.logger.__id__
t.logger =  Logger.new(STDERR)

# class logger should be same
puts "Class Logger is #{Test.logger.__id__ == lg ? 'still the' : 'not'} same"

# class and instance logger should be different
puts "Loggers are #{t.logger.__id__ == Test.logger.__id__ ? '' : 'not '}the same"

执行时:

➜沙盒irb
1.9.3-p392:001 > 加载“test_l.rb”
实例类 REF ID70203753590760
类实例 REF ID 70203753590500
类 ID 70203753590500
类实例 REF ID 70203753590500

记录器不一样#我希望是一样的...... :(

实例类 REF ID70203753590760
类实例 REF ID 70203753590000
类 ID 70203753590000
类实例 REF ID 70203753590000

记录器不一样#我希望是一样的...... :(

类实例 REF ID 70203753590000
类实例 REF ID 70203753590000

类 Logger 还是一样的

类实例 REF ID 70203753590000

记录器不一样
4

1 回答 1

6

我故意忘记了如何使用@@变量,因为它们非常混乱而且很少需要。

相反,只考虑使用实例变量,但如果需要,可以委托给类级别:

module Logging
  attr_writer :logger

  def logger
    defined?(@logger) ? @logger : self.class.logger
  end

  module ClassMethods
    def logger=(logger)
      @logger = logger
    end

    def logger
      @logger ||= Logger.new(STDERR)
    end
  end

  def self.included(base)
    base.extend(ClassMethods)
  end
end

class Test
  include Logging

  # ...
end
于 2013-03-07T14:46:21.047 回答