Slide 1

Slide 1 text

CLASS INSTANCE VARIABLES RZESZÓW RUBY USER GROUP #2 30.11.2016 Maciej Rząsa PGS Software @mjrzasa

Slide 2

Slide 2 text

KLASOWE CZY INSTANCYJNE?

Slide 3

Slide 3 text

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"

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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!

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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 ]

Slide 10

Slide 10 text

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 ]

Slide 11

Slide 11 text

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