Skip to content

Instantly share code, notes, and snippets.

@sachsy
Forked from mcordell/class_instance_vars.rb
Created January 28, 2016 15:15
Show Gist options
  • Save sachsy/ae54b31bbf0da5c71b62 to your computer and use it in GitHub Desktop.
Save sachsy/ae54b31bbf0da5c71b62 to your computer and use it in GitHub Desktop.
Class Instance Variables in Ruby.
class ClassVarTest
@variable = "Class Variable"
def self.get_class_variable
@variable
end
def get_object_variable
@variable
end
def define_variable
@variable = "Instance Variable"
end
end
> ClassVarTest.get_class_variable
=> "Class Variable"
> object = ClassVarTest.new
> object.get_object_variable
=> nil
> object.define_variable
> object.get_object_variable
=> "Instance Variable"
> ClassVarTest.get_class_variable
=> "Class Variable"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment