[PL] Class instance variables. RRUG #2

Ba17945a06aac247b06548d5afe341e8?s=47 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).

Ba17945a06aac247b06548d5afe341e8?s=128

mrzasa

November 30, 2016
Tweet

Transcript

  1. CLASS INSTANCE VARIABLES RZESZÓW RUBY USER GROUP #2 30.11.2016 Maciej

    Rząsa PGS Software @mjrzasa
  2. KLASOWE CZY INSTANCYJNE?

  3. 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"
  4. 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
  5. 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
  6. 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...
  7. 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!
  8. 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
  9. 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 ]
  10. 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 ]
  11. 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