Skip to content

Instantly share code, notes, and snippets.

@danlynn
Last active March 3, 2016 23:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danlynn/cd770a0960d2470571f0 to your computer and use it in GitHub Desktop.
Save danlynn/cd770a0960d2470571f0 to your computer and use it in GitHub Desktop.
$global_var = 'global'
class Foo
def self.class_var # note name start with 'self.' making this a class-level method
@@class_var
# would be error if tried to access @instance_var here since class knows nothing about any particular instance of class
end
def instance_var
@instance_var
end
def both_instance_and_class
[@instance_var, @@class_var]
end
def initialize
@instance_var = 'instance'
@@class_var = 'class'
local_var = 'local' # does not exist after this block of code is exited
end
end
foo = Foo.new
foo.instance_var #=> "instance"
Foo.class_var #=> "class"
foo.both_instance_and_class #=> ["instance", "class"]
$global_var #=> "global"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment