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 X | |
attr_accessor :foo | |
def [](key) | |
__send__ key.to_sym if [:foo].include? key.to_sym | |
end | |
def []=(key, value) | |
__send__ :"#{key}=", value if [:foo].include? key.to_sym | |
end | |
end | |
x = X.new | |
x[:foo] = 123 | |
puts x[:foo] | |
x.foo = "works" | |
p x.foo |
No comments:
Post a Comment