Share
Embed
[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
Transcript
11 slides
-
CLASS INSTANCE VARIABLES RZESZÓW RUBY USER GROUP #2 30.11.2016 Maciej
Rząsa PGS Software @mjrzasa -
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" -
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 -
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... -
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! -
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 -
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 ] -
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 ] -
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