Skip to content

Instantly share code, notes, and snippets.

@evie404
Last active December 21, 2015 05:18
Show Gist options
  • Save evie404/6255529 to your computer and use it in GitHub Desktop.
Save evie404/6255529 to your computer and use it in GitHub Desktop.
Class variable propagates to child classes in Ruby
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