Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mxriverlynn/273675 to your computer and use it in GitHub Desktop.
Save mxriverlynn/273675 to your computer and use it in GitHub Desktop.
# the verbose, annoying to maintain way :(
module MyModule
@@my_attribute = nil
def MyModule.my_attribute=(value)
@@my_attribute = value
end
def MyModule.my_attribute
@@my_attribute
end
end
#---- usage
MyModule::my_attribute = "some value"
puts MyModule::my_attribute #=> "some value"
# the metaclass way :)
module MyModule
class << self
attr_accessor :my_attribute
end
end
#---- usage
MyModule::my_attribute = "some value"
puts MyModule::my_attribute #=> "some value"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment