Challenge - 5 Problems
Ruby Parent Method Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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
Attempts:
2 left
💡 Hint
Remember that
super calls the parent method with the same name.✗ Incorrect
The
greet method in Child calls super, which runs Parent#greet returning "Hello from Parent". Then it adds " and Child" to that string.❓ Predict Output
intermediate2: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)
Attempts:
2 left
💡 Hint
Check how
super passes arguments to the parent method.✗ Incorrect
The
multiply method in Child calls super with arguments 3 and 4, which returns 12. Then it adds 10, resulting in 22.🔧 Debug
advanced2: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
Attempts:
2 left
💡 Hint
Calling
super() with empty parentheses calls the parent method with no arguments.✗ Incorrect
The parent method
show takes no arguments. Calling super() correctly calls it without arguments, so it returns "Parent show" without error.❓ Predict Output
advanced2: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
Attempts:
2 left
💡 Hint
When calling
super without arguments, Ruby passes the current method's arguments to the parent.✗ Incorrect
The
info method in Child has default argument "Child" and calls super without arguments, so it passes the current method's argument (which defaults to "Child") to Parent#info. Therefore, the output is "Hello, Child".🧠 Conceptual
expert2: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
Attempts:
2 left
💡 Hint
Check what
yield returns and what process returns.✗ Incorrect
The
process method in Parent yields to the block, but the block's return value is ignored. The method then returns "Parent done". The Child method calls super with a block, captures the return value "Parent done", and returns it.