Skip to content

Instantly share code, notes, and snippets.

@carlhuda
Created February 13, 2010 07:22
Show Gist options
  • Save carlhuda/303313 to your computer and use it in GitHub Desktop.
Save carlhuda/303313 to your computer and use it in GitHub Desktop.
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