Recall & Review
beginner
What keyword in Ruby is used to call a method from the parent class?
The <code>super</code> keyword is used to call a method from the parent class in Ruby.Click to reveal answer
intermediate
How does
super behave when called without arguments inside a method?When
super is called without arguments, it passes all the arguments received by the current method to the parent method.Click to reveal answer
intermediate
What happens if you call
super() with empty parentheses in a method?Calling
super() with empty parentheses calls the parent method without passing any arguments, even if the current method received some.Click to reveal answer
advanced
Can
super be used to access parent methods with different names?No,
super calls the parent method with the same name as the current method. To call a differently named parent method, you must call it explicitly with ParentClassName.method_name.Click to reveal answer
beginner
Example: How to override a method and still use the parent method's behavior in Ruby?
You override the method in the child class and inside it call <code>super</code> to reuse the parent method's code. For example:<br><pre>class Parent
def greet
"Hello"
end
end
class Child < Parent
def greet
super + ", world!"
end
end
Child.new.greet # => "Hello, world!"</pre>Click to reveal answer
In Ruby, what does
super do inside a method?✗ Incorrect
super calls the parent class's method that has the same name as the current method.
What happens if you call
super() with empty parentheses in a Ruby method?✗ Incorrect
super() calls the parent method without passing any arguments.
If a child method receives arguments but calls
super without parentheses, what happens?✗ Incorrect
Calling super without parentheses passes the current method's arguments to the parent method.
How can you call a parent method with a different name than the current method?
✗ Incorrect
You must call the parent method explicitly by its class and method name.
What is the purpose of using
super in a child class method?✗ Incorrect
super helps reuse or extend the parent method's code inside the child method.
Explain how the
super keyword works in Ruby when overriding methods.Think about how you can reuse code from the parent method inside the child method.
You got /3 concepts.
Describe how to call a parent class method that has a different name than the current method in Ruby.
Remember that super only calls methods with the same name.
You got /3 concepts.