Skip to content

Instantly share code, notes, and snippets.

@djspinmonkey
Created November 7, 2011 19:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save djspinmonkey/1345883 to your computer and use it in GitHub Desktop.
Save djspinmonkey/1345883 to your computer and use it in GitHub Desktop.
An implementation of destruct without using eval or #binding
def assert( bool, msg )
puts (bool ? "Pass " : "Fail ") + msg
end
class Hash
def destruct( &block )
vals = block.parameters.map { |p| self[p[1]] }
block.call(*vals)
end
end
foo = nil
hash = {foo: 1, bar: 2}
hash.destruct do |foo|
assert( foo == 1, "inside destruct" )
end
assert foo.nil?, "outside destruct"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment