如何避免类和全局变量

时间:2015-09-29 10:49:23

标签: ruby variables memory scope coding-style

RubocopThe Ruby Style Guide确认。它不鼓励使用除实例变量之外的任何东西。我发现不使用至少类变量会让人感到困惑。样式指南中的这个片段对全局变量的使用不以为然,而是推荐模块实例变量

# bad
$foo_bar = 1

# good
module Foo
  class << self
    attr_accessor :bar
  end
end

Foo.bar = 1

警惕使用全局变量是有意义的,但是既不使用全局变量也不使用类变量会让我大吃一惊。

模块实例变量类实例变量中,哪种内存使用效率更高?

例如:

选项A(类实例变量):

# things that exist only with life
module Life
  # an instance of life with unique actions/attributes
  class Person
    attr_accessor :memories

    def initialize
      @memories = []
    end

    def memorize(something)
      @memories << something
    end
  end
end

bob = Life::Person.new
bob.memorize 'birthday'
bob.memorize 'wedding'
bob.memorize 'anniversary'

bob.memories
# => ["birthday", "wedding", "anniversary"]

选项B(模块实例变量):

# things that exist only with life
module Life
  # something all living things possess
  module Memory
    class << self
      attr_accessor :memories
    end
  end

  # an instance of life with unique actions/attributes
  class Person
    include Memory

    def initialize
      Memory.memories = []
    end

    def memorize(something)
      Memory.memories << something
    end

    def memories
      Memory.memories
    end
  end
end

bob = Life::Person.new
bob.memorize 'birthday'
bob.memorize 'wedding'
bob.memorize 'anniversary'

bob.memories
# => ["birthday", "wedding", "anniversary"]

1 个答案:

答案 0 :(得分:3)

你误解了术语“类实例变量”。它表示“Class对象上的实例变量”,而不是“某个类的实例上的实例变量”。

  class Person
    attr_accessor :memories # instance variable, not shared

    class << self
      attr_accessor :memories # class instance variable, shared between
                              # all instances of this class
    end
  end

显然,有时你需要使用类实例变量。不要使用类变量(@@memories),因为它们在层次结构中的所有类(类及其子类)之间共享,这可能会导致令人惊讶的行为。