Created
July 22, 2013 19:34
-
-
Save zachgersh/6056902 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# 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