Super keyword in Ruby

Ruby

Ruby Problem Overview


What is the super for in this code?

def initialize options = {}, &block
  @filter = options.delete(:filter) || 1
  super
end

As far as I know it's like calling the function recursively, right?

Ruby Solutions


Solution 1 - Ruby

no... super calls the method of the parent class, if it exists. Also, as @EnabrenTane pointed out, it passes all the arguments to the parent class method as well.

Solution 2 - Ruby

super calls a parent method of the same name, with the same arguments. It's very useful to use for inherited classes.

Here's an example:

class Foo
  def baz(str)
    p 'parent with ' + str
  end
end

class Bar < Foo
  def baz(str)
    super
    p 'child with ' + str
  end
end

Bar.new.baz('test') # => 'parent with test' \ 'child with test'

There's no limit to how many times you can call super, so it's possible to use it with multiple inherited classes, like this:

class Foo
  def gazonk(str)
    p 'parent with ' + str
  end
end

class Bar < Foo
  def gazonk(str)
    super
    p 'child with ' + str
  end
end

class Baz < Bar
  def gazonk(str)
    super
    p 'grandchild with ' + str
  end
end

Baz.new.gazonk('test') # => 'parent with test' \ 'child with test' \ 'grandchild with test'

If there's no parent method of the same name, however, Ruby raises an exception:

class Foo; end

class Bar < Foo
  def baz(str)
    super
    p 'child with ' + str
  end
end

Bar.new.baz('test') # => NoMethodError: super: no superclass method ‘baz’

Solution 3 - Ruby

The super keyword can be used to call a method of the same name in the superclass of the class making the call.

It passes all the arguments to parent class method.

super is not same as super()

class Foo
  def show
   puts "Foo#show"
  end
end

class Bar < Foo
  def show(text)
   super

   puts text
  end
end


Bar.new.show("Hello Ruby")

ArgumentError: wrong number of arguments (1 for 0)

super(without parentheses) within subclass will call parent method with exactly same arguments that were passed to original method (so super inside Bar#show becomes super("Hello Ruby") and causing error because parent method does not takes any argument)

Solution 4 - Ruby

I know this is late but:

super method calls the parent class method.

for example:

class A
  def a
    # do stuff for A
  end
end

class B < A
  def a
    # do some stuff specific to B
    super
    # or use super() if you don't want super to pass on any args that method a might have had
    # super/super() can also be called first
    # it should be noted that some design patterns call for avoiding this construct
    # as it creates a tight coupling between the classes.  If you control both
    # classes, it's not as big a deal, but if the superclass is outside your control
    # it could change, w/o you knowing.  This is pretty much composition vs inheritance
  end
end

If it is not enough then you can study further from here

Solution 5 - Ruby

Bonus:

module Bar
    def self.included base
        base.extend ClassMethods
    end

    module ClassMethods
        def bar
            "bar in Bar"
        end
    end
end

class Foo
    include Bar
    class << self
        def bar
            super
        end
    end
end

puts Foo.bar # => "bar in Bar"

Solution 6 - Ruby

Super in a method of a class , say test_method, is used to call another method with same name i.e test_method of a parent class. The code written above and below the super keyword will be executed normally and the whole bunch of code action of the method of super class will be included at the place of super keyword.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmabounassifView Question on Stackoverflow
Solution 1 - RubysethvargoView Answer on Stackoverflow
Solution 2 - RubyvonconradView Answer on Stackoverflow
Solution 3 - RubycvibhaView Answer on Stackoverflow
Solution 4 - RubyAbdul BaigView Answer on Stackoverflow
Solution 5 - RubyDaniel CairolView Answer on Stackoverflow
Solution 6 - RubyPraveen PandeyView Answer on Stackoverflow