Skip to content

Instantly share code, notes, and snippets.

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 zachgersh/6056902 to your computer and use it in GitHub Desktop.
Save zachgersh/6056902 to your computer and use it in GitHub Desktop.
# Solution for Challenge: Ruby Drill: Exploring Scope. Started 2013-07-22T18:55:03+00:00
THIS_IS_A_CONSTANT = "constantS"
$global_var = "This is my global variable. Don't Do THIS!!!"
def get_constant
THIS_IS_A_CONSTANT
end
def get_global
$global_var
end
# Ruby doesn't like this
# def set_constant=(word)
# THIS_IS_A_constant = word
# end
def set_global=(word)
$global_var = word
end
local_var = "local variable in GLOBAL scope - MARK"
def get_local_variable
local_var
end
class BasicClass
def initialize
@instance_var = "instance variable in BasicClass - For Mark"
@@class_var = "HEYYYYYYYYYYYYYYYYYYYYYYY"
end
attr_accessor :class_var
# local_var not defined in this scope (it's local to global)
# def get_local_variable
# local_var
# end
def get_instance_variable
@instance_var
end
def set_instance_var=(word)
@instance_var = word
end
# You cannot accesss this constant from within the class
def get_constant
THIS_IS_A_CONSTANT
end
def get_global
$global_var
end
# Ruby doesn't like this
# def set_constant=(word)
# THIS_IS_A_CONSTANT = word
# end
# Ruby doesn't like this
# def set_global=(word)
# $global_var = word
# end
end
test_basic_class = BasicClass.new
p test_basic_class.get_instance_variable
# This is no fun. YOU CANNOT DO THIS
# test_basic_class.get_local_variable
test_basic_class.set_instance_var = "foo"
p test_basic_class.get_instance_variable
second_basic_class = BasicClass.new
second_basic_class.set_instance_var = "TODAY"
second_basic_class.get_instance_variable
test_basic_class.get_instance_variable
p get_constant
p get_global
p test_basic_class.get_constant
p test_basic_class.get_global
# YOU CANNOT DO THIS. DO NOT REASSIGN CONSTANTS. NO. BAD
# test_basic_class.set_constant = "zach can't spell"
# p test_basic_class.get_constant
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment