Upgrade to Pro — share decks privately, control downloads, hide ads and more …

[PL] Class instance variables. RRUG #2

mrzasa
November 30, 2016

[PL] Class instance variables. RRUG #2

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).

mrzasa

November 30, 2016
Tweet

More Decks by mrzasa

Other Decks in Programming

Transcript

  1. ZMIENNE INSTANCYJNE class Book attr_accessor :author, :title def info "#{@author}:

    #{@title}" end end b1 = Book.new b1.author = "Adam Mickiewicz" b1.title = "Dziady" b1.info # => "Adam Mickiewicz: Dziady" b2 = Book.new b2.author = "Jan Kochanowski" b2.title = "Pieśni" b2.info # => "Jan Kochanowski: Pieśni"
  2. 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
  3. 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
  4. ZMIENNE KLASOWE: DZIEDZICZENIE class Book def self.available_genres(*genres) @@available_genres = genres

    end def self.show_available_genres @@available_genres end available_genres :novel, :poetry, :drama end Book.show_available_genres # => :novel, :poetry, :drama class Novel < Book available_genres :historical, :contemporary, :futuristic end Novel.show_available_genres # => :historical, :contemporary, :futuristic # => yay! Book.show_available_genres # => :historical, :contemporary, :futuristic # => ups...
  5. KLASOWE ZMIENNE INSTANCYJNE class Book def self.available_genres(*genres) @available_genres = genres

    end def self.show_available_genres @available_genres end available_genres :novel, :poetry, :drama end Book.show_available_genres # => :novel, :poetry, :drama class Novel < Book available_genres :historical, :contemporary, :futuristic end Novel.show_available_genres # => :historical, :contemporary, :futuristic # => yay! Book.available_genres # => :novel, :poetry, :drama # => yay!
  6. 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
  7. W MODUŁACH module Options def available_options(*options) @@available_options = options end

    def show_available_options @@available_options end end class Book extend Options available_options :novel, :drama, :poetry end Book.show_available_options # [ :novel, :drama, :poetry ] class Job extend Options available_options :developer, :tester, :manager end Job.show_available_options # => [ :developer, :tester, :manager ] Book.show_available_options # => [ :developer, :tester, :manager ]
  8. W MODUŁACH module Options def available_options(*options) @available_options = options end

    def show_available_options @available_options end end class Book extend Options available_options :novel, :drama, :poetry end Book.show_available_options # => [ :novel, :drama, :poetry ] class Job extend Options available_options :developer, :tester, :manager end Job.show_available_options # => [ :developer, :tester, :manager ] Book.show_available_options # => [ :novel, :drama, :poetry ]
  9. 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