Created
May 3, 2010 13:41
-
-
Save neerajsingh0101/388099 to your computer and use it in GitHub Desktop.
This file contains 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 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