-
-
Save sorah/4657188 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Foo | |
# Defining class variable on Foo | |
@@bar = :alice | |
def hello | |
"Hello, #{@@bar}" | |
end | |
def bar=(x) | |
@@bar = x | |
end | |
end | |
foo_a = Foo.new | |
p foo_a.hello #=> "Hello, alice" | |
foo_b = Foo.new | |
# Foo#bar method replaces class variable @@bar. | |
foo_b.bar = :bob | |
# So, this effects to other instance of class Foo. | |
p foo_a.hello #=> "Hello, bob" | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Foo | |
@@bar = :alice | |
end | |
# Declare new class Bar inherits Foo | |
class Bar < Foo | |
# You can see superclass' class variable. | |
p @@bar #=> :alice | |
# Try to replace in subclass | |
@@bar = :john | |
end | |
class Foo | |
# Above line effects to its superclass, Foo! | |
p @@bar #=> :john | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Foo | |
end | |
class Bar < Foo | |
@@bar = :john | |
end | |
class Foo | |
# Similarly, you can replace subclass' class variable from superclass. | |
@@bar = :hello | |
end | |
class Bar | |
p @@bar #=> :hello | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Foo | |
class << self | |
def name | |
# At here `self` is class Foo, so, instance variable will be | |
# defined at class Foo. | |
@name ||= :alice # default value | |
end | |
def name=(x) | |
@name = x | |
end | |
end | |
end | |
p Foo.name #=> :alice | |
Foo.name = :bob | |
p Foo.name #=> :bob | |
p Foo.instance_variables #=> [:@name] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Foo | |
class << self | |
def name | |
# At here `self` is class Foo, so, instance variable will be | |
# defined at class Foo. | |
@name ||= :alice # default value | |
end | |
def name=(x) | |
@name = x | |
end | |
end | |
end | |
class Bar < Foo | |
end | |
Foo.name = :bob | |
# Because :bob will be placed in Foo class' instance variable, | |
# so above line doesn't effect to class Bar. | |
p Bar.name #=> :alice | |
p Foo.name #=> :bob |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Foo | |
class << self | |
attr_accessor :name | |
end | |
end | |
Foo.name = :alice | |
p Foo.name #=> :alice |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Foo | |
class << self | |
attr_accessor :name | |
end | |
def hello | |
# look up using accessor declared on Foo class | |
"Hello, #{self.class.name}" | |
end | |
end | |
Foo.name = :alice | |
p Foo.new.hello #=> "Hello, alice" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Foo | |
class << self | |
# Declare writer only | |
attr_writer :name | |
end | |
def hello | |
"Hello, #{name}" | |
end | |
private | |
def name | |
# Look up using Object#instance_variable_get. | |
self.class.instance_variable_get(:@name) | |
end | |
end | |
Foo.name = :alice | |
p Foo.new.hello #=> "Hello, alice" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment