Skip to content

Instantly share code, notes, and snippets.

@keithrbennett
Created January 19, 2011 16:08
Show Gist options
  • Select an option

  • Save keithrbennett/786369 to your computer and use it in GitHub Desktop.

Select an option

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.
# 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