Created
March 14, 2016 16:12
-
-
Save tiagopog/7cd65e81e90735246dfd to your computer and use it in GitHub Desktop.
Ruby's define_method
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
| class A | |
| def fred | |
| puts "In Fred" | |
| end | |
| def create_method(name, &block) | |
| self.class.send(:define_method, name, &block) | |
| end | |
| define_method(:wilma) { puts "Charge it!" } | |
| end | |
| class B < A | |
| define_method(:barney, instance_method(:fred)) | |
| end | |
| a = B.new | |
| a.barney | |
| a.wilma | |
| a.create_method(:betty) { p self } | |
| a.betty |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment