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. CLASS INSTANCE VARIABLES
    RZESZÓW RUBY USER GROUP #2
    30.11.2016
    Maciej Rząsa
    PGS Software
    @mjrzasa

    View Slide

  2. KLASOWE CZY
    INSTANCYJNE?

    View Slide

  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"

    View Slide

  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

    View Slide

  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

    View Slide

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

    View Slide

  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!

    View Slide

  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

    View Slide

  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 ]

    View Slide

  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 ]

    View Slide

  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

    View Slide