Skip to content

Instantly share code, notes, and snippets.

@betawaffle
Created March 23, 2012 13:09
Show Gist options
  • Save betawaffle/2170487 to your computer and use it in GitHub Desktop.
Save betawaffle/2170487 to your computer and use it in GitHub Desktop.
# Allows you to easily make a full closure in ruby
# Examples:
def bork
x = 'abc'
closure { x }
end
f = bork
f.call
# => 'abc'
f.call << ' def'
# => 'abc def'
f.call
# => 'abc def'
g = bork
g.call
# => 'abc'
f.call
# => 'abc def'
module Kernel
def closure &body
klass = class << self; self; end
klass.class_exec(body) do |body|
define_method('', &body) #.tap { remove_method('') }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment