0
0
Rubyprogramming~20 mins

Accessing parent methods in Ruby - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Ruby Parent Method Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of calling super in overridden method
What is the output of this Ruby code when Child.new.greet is called?
Ruby
class Parent
  def greet
    "Hello from Parent"
  end
end

class Child < Parent
  def greet
    super + " and Child"
  end
end

puts Child.new.greet
A"Hello from Parent"
B"Hello from Child"
C"Hello from Parent and Child"
DNoMethodError
Attempts:
2 left
💡 Hint
Remember that super calls the parent method with the same name.
Predict Output
intermediate
2:00remaining
Calling parent method with arguments
What will be printed when this Ruby code runs?
Ruby
class Parent
  def multiply(x, y)
    x * y
  end
end

class Child < Parent
  def multiply(x, y)
    super(x, y) + 10
  end
end

puts Child.new.multiply(3, 4)
A22
B12
C34
DArgumentError
Attempts:
2 left
💡 Hint
Check how super passes arguments to the parent method.
🔧 Debug
advanced
2:00remaining
Identify the error when calling parent method
What error will this Ruby code raise when Child.new.show is called?
Ruby
class Parent
  def show
    "Parent show"
  end
end

class Child < Parent
  def show
    super()
  end
end

Child.new.show
ANoMethodError
BTypeError
CArgumentError
DNo error, returns "Parent show"
Attempts:
2 left
💡 Hint
Calling super() with empty parentheses calls the parent method with no arguments.
Predict Output
advanced
2:00remaining
Using super with default arguments
What is the output of this Ruby code?
Ruby
class Parent
  def info(name = "Parent")
    "Hello, #{name}"
  end
end

class Child < Parent
  def info(name = "Child")
    super
  end
end

puts Child.new.info
AArgumentError
B"Hello, Child"
C"Hello, "
D"Hello, Parent"
Attempts:
2 left
💡 Hint
When calling super without arguments, Ruby passes the current method's arguments to the parent.
🧠 Conceptual
expert
2:00remaining
Understanding super with blocks
Consider this Ruby code. What will be the output when Child.new.process is called?
Ruby
class Parent
  def process
    yield if block_given?
    "Parent done"
  end
end

class Child < Parent
  def process
    result = super do
      "Block from Child"
    end
    result
  end
end

puts Child.new.process
A"Parent done"
B"Block from Child"
CLocalJumpError
Dnil
Attempts:
2 left
💡 Hint
Check what yield returns and what process returns.