Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to call the parent method from the child class.
Ruby
class Parent def greet puts "Hello from Parent" end end class Child < Parent def greet [1] end end Child.new.greet
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using self.greet calls the method on the current object, causing recursion.
Calling Parent.greet without an instance will cause an error.
Using greet alone calls the current method, causing infinite recursion.
✗ Incorrect
In Ruby, super calls the same method from the parent class.
2fill in blank
mediumComplete the code to call the parent method with arguments.
Ruby
class Parent def greet(name) puts "Hello, #{name} from Parent" end end class Child < Parent def greet(name) [1] end end Child.new.greet("Alice")
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
super without arguments passes all arguments automatically, but here explicit passing is clearer.Using
super() calls the parent method without arguments, causing errors.Using
super.greet(name) is invalid syntax.✗ Incorrect
Use super(name) to pass the argument to the parent method.
3fill in blank
hardFix the error in calling the parent method inside a module included in a class.
Ruby
module Mixin def greet [1] end end class Parent def greet puts "Hello from Parent" end end class Child < Parent include Mixin end Child.new.greet
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Calling Parent.greet directly causes an error because it's an instance method.
Using self.greet causes infinite recursion.
Using super() without arguments is valid but same as super here.
✗ Incorrect
Inside a module method, super calls the next method in the lookup chain, here the parent class method.
4fill in blank
hardFill the blank to call the parent method and add extra behavior.
Ruby
class Parent def greet puts "Hello from Parent" end end class Child < Parent def greet [1] puts "And hello from Child" end end Child.new.greet
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using self.greet causes infinite recursion.
Calling Parent.greet directly is invalid without an instance.
✗ Incorrect
Use super or super() to call the parent method before adding child behavior.
5fill in blank
hardFill the blank to override a method, call the parent method with arguments, and add extra output.
Ruby
class Parent def greet(name) puts "Hello, #{name} from Parent" end end class Child < Parent def greet(name) [1] puts "And hello from Child to #{name}" end end Child.new.greet("Bob")
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
self.greet(name) causes infinite recursion.Using
super() without arguments causes errors here.✗ Incorrect
Use super(name) to call the parent method with argument.