Created
January 19, 2011 16:08
-
-
Save keithrbennett/786369 to your computer and use it in GitHub Desktop.
Shows that a derived class' initialize passes its args to super's
initialize when super is called without any args.
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
| # Shows that a derived class' initialize passes its args to super's | |
| # initialize when super is called without any args. | |
| class Base | |
| attr_accessor :foo | |
| def initialize(foo='base') | |
| @foo = foo | |
| end | |
| def to_s | |
| "foo: #{foo}" | |
| end | |
| end | |
| class Derived < Base | |
| def initialize(foo = "derived") | |
| super # invisibly passes foo to super's initialize | |
| end | |
| end | |
| puts Derived.new | |
| # foo: derived | |
| puts Derived.new("something else") | |
| # foo: something else |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment