Last active
October 5, 2020 07:45
-
-
Save inopinatus/8a0e2fcae015b985ef928b9c67d47938 to your computer and use it in GitHub Desktop.
excessively dynamic accessors
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
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