Created
January 22, 2013 13:19
-
-
Save mikhailbortnyk/4594620 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
--- ~ » irb | |
1.9.3p286 :001 > class A | |
1.9.3p286 :002?> def to_modify | |
1.9.3p286 :003?> "Not modified" | |
1.9.3p286 :004?> end | |
1.9.3p286 :005?> end | |
=> nil | |
1.9.3p286 :006 > def redefine_method(klass, &block) | |
1.9.3p286 :007?> if klass.new.respond_to?(:to_modify) | |
1.9.3p286 :008?> klass.send(:define_method, :to_modify, &block) | |
1.9.3p286 :009?> end | |
1.9.3p286 :010?> end | |
=> nil | |
1.9.3p286 :011 > A.new.to_modify | |
=> "Not modified" | |
1.9.3p286 :012 > redefine_method(A) do | |
1.9.3p286 :013 > puts "modified" | |
1.9.3p286 :014?> end | |
=> #<Proc:0x007f945409bfb0@(irb):12 (lambda)> | |
1.9.3p286 :015 > A.new.to_modify | |
modified | |
=> nil |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment