Created
February 13, 2010 07:22
-
-
Save carlhuda/303313 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
module TopLevel | |
class Foo | |
def to_s | |
"<An instance of Foo>" | |
end | |
alias inspect to_s | |
class << self | |
def to_s | |
"<Foo's metaclass>" | |
end | |
class << self | |
def to_s | |
"<Foo's metaclass>'s metaclass" | |
end | |
end | |
end | |
end | |
class Foo | |
puts "class Foo" | |
puts "self: #{self}" | |
puts "nesting: #{Module.nesting.inspect}" | |
def context | |
"Method definition context: #{self}'s metaclass" | |
end | |
puts context rescue puts "Method definition context: #{self}" | |
end | |
puts | |
Foo.class_eval do | |
puts "Foo.class_eval" | |
puts "self: #{self}" | |
puts "nesting: #{Module.nesting.inspect}" | |
def context | |
"Method definition context: #{self}'s metaclass" | |
end | |
puts context rescue puts "Method definition context: #{self}" | |
end | |
puts | |
class << Foo | |
puts "class << Foo" | |
puts "self: #{self}" | |
puts "nesting: #{Module.nesting.inspect}" | |
def context | |
"Method definition context: #{self}'s metaclass" | |
end | |
puts context rescue puts "Method definition context: #{self}" | |
end | |
puts | |
Foo.instance_eval do | |
puts "Foo.instance_eval" | |
puts "self: #{self}" | |
puts "nesting: #{Module.nesting.inspect}" | |
def context | |
"Method definition context: #{self}'s metaclass" | |
end | |
puts context rescue puts "Method definition context: #{self}" | |
end | |
puts | |
class << Foo.new | |
puts "class << Foo.new" | |
puts "self: #{self}" | |
puts "nesting: #{Module.nesting.inspect}" | |
def context | |
"Method definition context: #{self}'s metaclass" | |
end | |
puts context rescue puts "Method definition context: #{self}" | |
end | |
puts | |
Foo.new.instance_eval do | |
puts "Foo.new.instance_eval" | |
puts "self: #{self}" | |
puts "nesting: #{Module.nesting.inspect}" | |
def context | |
"Method definition context: #{self}'s metaclass" | |
end | |
puts context rescue puts "Method definition context: #{self}" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment