Challenge - 5 Problems
Super Keyword Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of super with arguments in method override
What is the output of the following Ruby code?
Ruby
class Parent def greet(name) "Hello, #{name}!" end end class Child < Parent def greet(name) super(name).upcase end end puts Child.new.greet("Alice")
Attempts:
2 left
💡 Hint
Remember that super(name) calls the parent method with the same argument, then upcase converts the string to uppercase.
✗ Incorrect
The Child class overrides greet and calls super(name) to invoke Parent#greet with the argument. The returned string is then converted to uppercase, resulting in 'HELLO, ALICE!'.
❓ Predict Output
intermediate2:00remaining
Effect of super without arguments in method override
What will this Ruby code print?
Ruby
class A def info(x) "Value: #{x}" end end class B < A def info(x) super + "!" end end puts B.new.info(5)
Attempts:
2 left
💡 Hint
Calling super without arguments passes the same arguments received by the method.
✗ Incorrect
In Ruby, calling super without parentheses passes all arguments received by the method to the parent method. So super calls info(5), returning 'Value: 5', then adds '!'.
❓ Predict Output
advanced2:00remaining
super with block forwarding behavior
What is the output of this Ruby code?
Ruby
class Base def process yield if block_given? end end class Derived < Base def process super { puts "Inside block" } end end Derived.new.process
Attempts:
2 left
💡 Hint
super forwards the block given to it, so the block inside super is executed.
✗ Incorrect
Derived#process calls super with a block that prints 'Inside block'. Base#process yields to the block, so 'Inside block' is printed.
❓ Predict Output
advanced2:00remaining
super with no arguments and no block in method with parameters
What happens when this Ruby code runs?
Ruby
class Parent def calculate(a, b) a + b end end class Child < Parent def calculate(a, b) super * 2 end end puts Child.new.calculate(3, 4)
Attempts:
2 left
💡 Hint
super without parentheses passes all arguments received by the method.
✗ Incorrect
Child#calculate calls super without parentheses, so it forwards both arguments (3,4) to Parent#calculate, which returns 7. Then 7 * 2 = 14 is printed.
❓ Predict Output
expert3:00remaining
super in multiple inheritance levels with keyword arguments
What is the output of this Ruby code?
Ruby
class Alpha def info(x:, y:) "Alpha: x=#{x}, y=#{y}" end end class Beta < Alpha def info(x:, y:) super(x: x + 1, y: y + 1) + " -> Beta" end end class Gamma < Beta def info(x:, y:) super(x: x * 2, y: y * 2) + " -> Gamma" end end puts Gamma.new.info(x: 1, y: 2)
Attempts:
2 left
💡 Hint
Each super call modifies the keyword arguments before passing them up the chain.
✗ Incorrect
Gamma#info calls super with x=2, y=4 (1*2, 2*2). Beta#info receives x=2, y=4, calls super with x=3, y=5 (2+1, 4+1). Alpha#info receives x=3, y=5 and returns 'Alpha: x=3, y=5'. Then Beta appends ' -> Beta', Gamma appends ' -> Gamma'.