Skip to content

Instantly share code, notes, and snippets.

@steveklabnik
Created December 7, 2011 13:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save steveklabnik/af3ea4ea1cfe4495b8e5 to your computer and use it in GitHub Desktop.
Save steveklabnik/af3ea4ea1cfe4495b8e5 to your computer and use it in GitHub Desktop.
fun with bindings
def with_binding(b)
vars = eval('local_variables', b)
vars.each do |v|
eval("#{v} = #{eval(v.to_s, b)}")
end
yield
end
a = 5
with_binding(binding()) do
puts a
end
@erithmetic
Copy link

Problem? :^)

@steveklabnik
Copy link
Author

Because it's the morning, I forgot that the block would already capture the binding...

def whatevs
  yield
end

a = 5

whatevs do
  puts a
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment