Difference between class variables and instance variables, how to use them properly with mixins. Talk given during a meeting of Rzeszów Ruby User Group (RRUG).
ZMIENNE KLASOWE class Book @@counter = 0 def initialize @@counter += 1 end def self.counter @@counter end def info @@type end end Book.new Book.new Book.counter # => 2
ZMIENNE KLASOWE: DZIEDZICZENIE class Book @@counter = 0 def initialize @@counter += 1 end def self.counter @@counter end def info @@type end end class Novel < Book def initialize super end end Book.new Book.new Book.counter # => 2 Novel.new Book.counter # => 3
PODSUMOWANIE zmienne klasowe @@var wspólne dla hierchii klas dostępne w instancjach bezpośrednio (@@var) klasowe zmienne instancyjne @var w ciele klasy albo metody klasowej prywatne dla danej klasy dostępne w klasie, w instancjach tylko przez metody klasowe
PODSUMOWANIE dwa typy zmiennych w klasie @@klasowe są wspólne dla hierarchii klas @instancyjne są prywatne dla klasy (bo klasa jest obiektem) ważne przy modułach dodających metody klasowe