Super Keyword in Ruby

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.

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.

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.

How can i understand Ruby super in the context of super(options.merge(include: :comments))?

First, your model extends ActiveRecord::Base, right? For example,

class Post < ActiveRecord::Base 
# ...
end

ActiveRecord::Base defines a as_json method. If you don't define def as_json in your model, you can still call as_json on an instance because it inherits that behavior from its superclass, ActiveRecord::Base.


Second, did you know that ActiveRecord::Base#as_json accepts some options? For example, using only: will restrict the JSON to the attributes you specify (doc).

Given that information, you could call as_json like this:

post.as_json(only: [:title, :published_at]) #=> {title: "...", published_at: "..."}

include: is one of the options which you can pass to as_json.


Third, super is used for calling the same method in a superclass. So, in this case:

class Post < ActiveRecord::Base 
def as_json(options = {})
super
end
end

super will call ActiveRecord::Base#as_json. That's good because that method defines this behavior!

Calling super with no arguments will simply pass the original arguments to that method call. So, these are equivalent:

def as_json(options = {})
# `options` hash is passed _implicitly_:
super
end

def as_json(options = {})
# `options` hash is passed _explicitly_:
super(options)
end

BUT, if you want, you can modify options before sending it to super. In your example, that's what is happening:

def as_json(options = {})
# add include: :comments to any provided options
new_options = options.merge(include: :comments)
# pass the modified options to super
super(new_options)
end

This way, you can be sure you're always passing include: :comments to ActiveRecord::Base's implementation of as_json!


Hope that helps!



Related Topics



Leave a reply



Submit