Last active
December 18, 2015 09:09
-
-
Save ilude/5759835 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
# object initialize is called | |
class NewClass | |
def initialize | |
end | |
end | |
# object initialize is called | |
class NewClass | |
def initialize | |
super | |
end | |
end |
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
class Foo | |
def initialize | |
puts "\t#{self.class} initializer called" | |
end | |
end | |
class Bar < Foo | |
def initialize | |
puts "\t#{self.class} initializer called" | |
end | |
end | |
class Baz < Foo | |
def initialize | |
puts "\t#{self.class} initializer called" | |
super | |
end | |
end | |
class BazFoo < Foo | |
end | |
puts "Creating new Bar!" | |
Bar.new | |
puts "Creating new Baz!" | |
Baz.new | |
puts "Creating new BazFoo!" | |
BazFoo.new |
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
Creating new Bar! | |
Bar initializer called | |
Creating new Baz! | |
Baz initializer called | |
Foo initializer called | |
Creating new BazFoo! | |
Foo initializer called |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
class Foo
def initialize
puts "Foo initializer called"
end
end
class Baz < Foo
def initialize
puts "Baz initializer called"
super
end
end
Baz.new
==> Baz initializer called
Foo initializer called
=> #Baz:0x007fdf5c90ea00
I thought super meant... "if the method super is called in appears in the superclass, then it inherits the same method found in the superclass."
why then would Baz.new puts "Baz initializer called" if that doesn't appear in the init method of it's superclass, Foo?