Skip to content

Instantly share code, notes, and snippets.

@brainopia
Created April 21, 2012 11:12
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 brainopia/2436656 to your computer and use it in GitHub Desktop.
Save brainopia/2436656 to your computer and use it in GitHub Desktop.
# eval with method definition is used to prevent warnings in some situations
DEFINEE = <<-CODE
eval 'def __; end'
it = method(:__).owner rescue instance_method(:__).owner
eval 'undef __'
it
CODE
self # => main
eval DEFINEE # => Object
class Foo
self # => Foo
eval DEFINEE # => Foo
def bar
self # => instance of Foo
eval DEFINEE # => Foo
end
end
Foo.instance_eval do
self # => Foo
eval DEFINEE # => singleton class of Foo
end
Foo.class_eval do
self # => Foo
eval DEFINEE # => Foo
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment