Last active
December 21, 2015 05:18
-
-
Save evie404/6255529 to your computer and use it in GitHub Desktop.
Class variable propagates to child classes in Ruby
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 Parent | |
def self.class_var | |
@@class_var ||= self.name | |
end | |
def self.instance_var | |
@instance_var ||= self.name | |
end | |
end | |
class Children < Parent | |
end | |
puts "Parent.class_var:\t#{Parent.class_var}" | |
puts "Children.class_var:\t#{Children.class_var}" | |
puts "" | |
puts "Parent.instance_var:\t#{Parent.instance_var}" | |
puts "Children.instance_var:\t#{Children.instance_var}" | |
# Parent.class_var: Parent | |
# Children.class_var: Parent | |
# | |
# Parent.instance_var: Parent | |
# Children.instance_var: Children |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment