Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to call the parent class method using super.
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' instead of 'super' to call the parent method.
Using 'parent' which is not a Ruby keyword.
Using 'super' without parentheses when arguments are expected.
✗ Incorrect
In Ruby, calling super() with empty parentheses calls the parent method with no arguments explicitly.
2fill in blank
mediumComplete the code to pass arguments to the parent method using super.
Ruby
class Parent def initialize(name) @name = name end end class Child < Parent def initialize(name, age) [1] @age = age end end child = Child.new("Alice", 10)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Calling super() without arguments when parent expects one.
Passing both name and age to super when parent expects only name.
Using super without parentheses which passes all arguments automatically.
✗ Incorrect
super(name) calls the parent initialize method passing the name argument.
3fill in blank
hardFix the error in calling super with arguments in the method below.
Ruby
class Parent def calculate(x, y) x + y end end class Child < Parent def calculate(x, y) result = [1] result * 2 end end child = Child.new puts child.calculate(3, 4)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Calling super() with no arguments when parent expects two.
Passing only one argument to super when two are needed.
Using super without parentheses which may cause confusion.
✗ Incorrect
super(x, y) correctly passes both arguments to the parent method.
4fill in blank
hardFill both blanks to correctly override and extend the parent method.
Ruby
class Parent def info "Parent info" end end class Child < Parent def info parent_info = [1] "Child info and " + [2] end end puts Child.new.info
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using info instead of super to call parent method.
Using super() without assigning to a variable.
Using the wrong variable name in string concatenation.
✗ Incorrect
Use super to get parent info and then use the variable parent_info to combine strings.
5fill in blank
hardFill all three blanks to correctly use super with arguments and extend the method.
Ruby
class Parent def multiply(a, b) a * b end end class Child < Parent def multiply(a, b) base = [1] addition = [2] + [3] base + addition end end puts Child.new.multiply(3, 4)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Calling super() without arguments when parent expects two.
Using wrong variables for addition.
Not calling super at all.
✗ Incorrect
super(a, b) calls the parent method with both arguments; then a and b are added to the result.