Last active
March 3, 2016 23:27
-
-
Save danlynn/cd770a0960d2470571f0 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
$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