Skip to content

Instantly share code, notes, and snippets.

@neerajsingh0101
Created May 3, 2010 13:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neerajsingh0101/388099 to your computer and use it in GitHub Desktop.
Save neerajsingh0101/388099 to your computer and use it in GitHub Desktop.
module Kernel
def singleton_class
class << self
self
end
end
end
class Human
proc = lambda { puts 'proc says my class is ' + self.name.to_s }
singleton_class.instance_eval do
define_method(:lab) do
proc.call
end
end
end
class Developer < Human
end
Human.lab # class is Human
Developer.lab # class is Human ; oops
# The fix is to use instance_exec
class Human
proc = lambda { puts 'proc says my class is ' + self.name.to_s }
singleton_class.instance_eval do
define_method(:lab) do
self.instance_exec &proc
end
end
end
class Developer < Human
end
Human.lab # class is Human
Developer.lab # class is Human ; oops
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment