Created
August 29, 2012 06:41
-
-
Save phiggins/3507548 to your computer and use it in GitHub Desktop.
Class configuration using method_missing
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 Awesome | |
@config = {} | |
def self.configure | |
yield(self) | |
end | |
def self.method_missing method, *args, &block | |
if method.to_s[-1] == '=' | |
@config[method[0..-2].to_sym] = args.first | |
else | |
@config[method] | |
end | |
end | |
end | |
Awesome.configure do |a| | |
a.magic_number = 3 | |
end | |
puts Awesome.magic_number | |
# => 3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment