Skip to content

Instantly share code, notes, and snippets.

@banister
Forked from JoshCheek/gist:835008
Created February 19, 2011 11:31
Show Gist options
  • Save banister/835013 to your computer and use it in GitHub Desktop.
Save banister/835013 to your computer and use it in GitHub Desktop.
module MyMod
# store the variable on the module itself
var = "initial value"
# first method that accesses the var
define_method :get_var do
var
end
# second method that accesses the var
define_method :set_var do |value|
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 # => []
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment