Created
July 1, 2012 16:24
-
-
Save jbrechtel/3028855 to your computer and use it in GitHub Desktop.
overriding new
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 Parent | |
| class << self | |
| alias_method :orig_new, :new | |
| def new(arg) | |
| puts "newing parent with #{arg}" | |
| orig_new | |
| end | |
| end | |
| def parent_method | |
| puts 'and inherits..' | |
| end | |
| end | |
| class YourChild < Parent | |
| def self.new | |
| super("child args") | |
| end | |
| def child_method | |
| puts 'see? really the child' | |
| end | |
| end | |
| puts YourChild.new.child_method | |
| puts YourChild.new.parent_method |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment