Ruby Super Keyword

Super keyword in 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.

What is `super` in Ruby?

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

What is the usage of super keyword here?

Here super keyword calls the same data method of its parent class if data_type == 'option' is false.
Check this link for a detailed explanation.
Super keyword in Ruby

Update:

The above code can be re-written as

if data_type == 'option'
options[super.to_i]#options is probably a hash/array here.
else
super
end

When we call super it returns a value after executing it's parent class's data method, lets assume it returned "5", we're converting that to an integer and getting data out of options array. i.e., options[5].

In the else block we're simply returning the the value parent's data method got us.

Ruby return statement does not work with super keyword?

super acts like a method call that calls the superclass's method implementation. In your example, the return keyword returns from Parent::test and continues executing Child::test, just like any other method call would.

What does the method super do in the ruby code?


I think I know super intends to override the same method inherited from the parent class.

Actually, it is the opposite. When you override some method in a child class, super is used to refer to the behaviour of the same method in the parent class (i.e., the original behaviour).

In the given code, the indent is to make it dynamically respondable to any method that starts with foo without changing the respondability of other methods. The first part is done by:

if method_name.to_s[0,3] == "foo"
true

but if it were only that, all other undefined methods would simply return nil even if they are defined in some parent class. If some parent class were to respond to such method, then returning nil to respondability would incorrectly block such methods. The other part:

else
super(method_name)

is to return the correct value for such case. It means that in other cases, do what the parent class did.

Why is 'super' a keyword rather than a method in Ruby?

It behaves a little differently, in that if you don't pass arguments, all of the current arguments (and block, if present) are passed along... I'm not sure how that would work as a method.

To give a rather contrived example:

class A
def example(a, b, c)
yield whatever(a, b) + c
end
end

class B < A
def example(a, b, c)
super * 2
end
end

I did not need to handle the yield, or pass the arguments to super. In the cases where you specifically want to pass different arguments, then it behaves more like a method call. If you want to pass no arguments at all, you must pass empty parentheses (super()).

It simply doesn't have quite the same behaviour as a method call.

Calling super, super class method with parameters in Ruby

You can pass parameters directly to call like this:

class Child < Parent
def fun(word)
GrandParent.instance_method(:fun).bind(self).call(param1, param2)
end
end


Related Topics



Leave a reply



Submit