Created
November 15, 2018 20:41
-
-
Save unixcharles/c0f08dfc3c3f479c810094abdfe77246 to your computer and use it in GitHub Desktop.
Hash Driven Development
This file contains hidden or 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 HashObject | |
def []=(method_name, implementation) | |
define_singleton_method(method_name, &implementation) | |
end | |
def [](method_name) | |
if method(method_name).arity == 0 | |
method(method_name).call | |
else | |
method(method_name).curry | |
end | |
end | |
end | |
NewHashObject = lambda { HashObject.new } | |
> hash_object = NewHashObject[] | |
=> #<HashObject:0x007f97c34d3098> | |
> hash_object[:test] = -> (foo, bar, baz) { [foo, bar, baz].join } | |
=> #<Proc:0x007f97c34c9e58@(irb):57 (lambda)> | |
> hash_object[:test]['foo']['bar']['baz'] | |
=> "foobarbaz" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment