public

Class configuration using method_missing

  • Download Gist
config_example_method_missing.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.