0
0
Rubyprogramming~10 mins

Super keyword behavior in Ruby - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
Aself
Bsuper()
Cparent
Dsuper
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.
2fill in blank
medium

Complete 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'
Asuper(name)
Bsuper()
Csuper(name, age)
Dsuper
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.
3fill in blank
hard

Fix 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'
Asuper(x, y)
Bsuper(x)
Csuper
Dsuper()
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.
4fill in blank
hard

Fill 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'
Asuper
Binfo
Cparent_info
Dsuper()
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.
5fill in blank
hard

Fill 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'
Asuper(a, b)
Ba
Cb
Dsuper()
Attempts:
3 left
💡 Hint
Common Mistakes
Calling super() without arguments when parent expects two.
Using wrong variables for addition.
Not calling super at all.