Created
March 23, 2012 13:09
-
-
Save betawaffle/2170487 to your computer and use it in GitHub Desktop.
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
# 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' |
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
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