Created
February 19, 2011 11:25
-
-
Save JoshCheek/835008 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
module MyMod | |
# store the variable on the module itself | |
@var = "initial value" | |
class << self | |
attr_accessor :var | |
end | |
# first method that accesses the var | |
def get_var | |
MyMod.var | |
end | |
# second method that accesses the var | |
def set_var value | |
MyMod.var = value | |
end | |
end | |
class MyClass | |
include MyMod | |
end | |
# show that we can use the methods | |
mc = MyClass.new | |
mc.get_var # => "initial value" | |
mc.set_var "altered value" | |
mc.get_var # => "altered value" | |
# so where is the variable stored? | |
mc.instance_variables # => [] | |
MyClass.instance_variables # => [] | |
MyMod.instance_variables # => [:@var] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment