Skip to content

Instantly share code, notes, and snippets.

@ilude
Last active December 18, 2015 09:09
Show Gist options
  • Save ilude/5759835 to your computer and use it in GitHub Desktop.
Save ilude/5759835 to your computer and use it in GitHub Desktop.
# object initialize is called
class NewClass
def initialize
end
end
# object initialize is called
class NewClass
def initialize
super
end
end
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
Creating new Bar!
Bar initializer called
Creating new Baz!
Baz initializer called
Foo initializer called
Creating new BazFoo!
Foo initializer called
@jbevarts
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment