Skip to content

Instantly share code, notes, and snippets.

@sandheepg
Last active March 8, 2017 07:20
Show Gist options
  • Select an option

  • Save sandheepg/bc9a9e17db7447537d32c5801465adc5 to your computer and use it in GitHub Desktop.

Select an option

Save sandheepg/bc9a9e17db7447537d32c5801465adc5 to your computer and use it in GitHub Desktop.
Calling super in initialize method of Ruby
class Person    
    def initialize(name)
        @name = name
    end    
end

class Employee < Person
end

emp = Employee.new # throws error wrong number of arguments

emp = Employee.new("foo") # parent class initialize method is called

class Employee < Person
    def initialize(name, designation)
        super(name)
        @designation = designation
    end
end

emp = Employee.new("foo", "bar")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment