Skip to content

Instantly share code, notes, and snippets.

@JoshCheek
Created February 19, 2011 11:25
Show Gist options
  • Save JoshCheek/835008 to your computer and use it in GitHub Desktop.
Save JoshCheek/835008 to your computer and use it in GitHub Desktop.
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