Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
excessively dynamic accessors
class MyConfiguration < BasicObject
def method_missing name, *args
getter = name.end_with?(?=) ? name[0..-2] : name
(class << self; self end).class_eval { attr_accessor getter }
__send__ name, *args
end
end
config = MyConfiguration.new
config.liverpool = 4
# [4, nil]
puts [config.liverpool, config.barcelona].inspect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment