Skip to content

Instantly share code, notes, and snippets.

@unixcharles
Created November 15, 2018 20:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save unixcharles/c0f08dfc3c3f479c810094abdfe77246 to your computer and use it in GitHub Desktop.
Save unixcharles/c0f08dfc3c3f479c810094abdfe77246 to your computer and use it in GitHub Desktop.
Hash Driven Development
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