Skip to content

Instantly share code, notes, and snippets.

@benolee
Last active December 14, 2015 10:09
Show Gist options
  • Select an option

  • Save benolee/5069861 to your computer and use it in GitHub Desktop.

Select an option

Save benolee/5069861 to your computer and use it in GitHub Desktop.
# slide 77 from waza talk
# method definition with static scope, class eval'd in a string
# Environment
class Foo
hello = "world!"
define_method("foo") { hello }
class_eval "def bar; hello; end"
end
x = Foo.new
x.foo # => "world!"
x.bar # => NameError
# method definition with closure, class_eval'd in a string
class Foo
hello = "world!"
define_method("foo") { hello }
class_eval 'define_method("bar") { hello }'
end
x = Foo.new
x.foo # => "world!"
x.bar # => "world!"
# RUBY_VERSION 1.9.2
# <3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment