Challenge - 5 Problems
Ruby Runtime Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Ruby method call and variable scope
What is the output of this Ruby code when run?
Ruby
def greet name = "Alice" puts "Hello, #{name}!" end greet puts name
Attempts:
2 left
💡 Hint
Think about where variables defined inside methods are accessible.
✗ Incorrect
The variable name is local to the greet method. It is not accessible outside the method, so puts name raises a NameError.
❓ Predict Output
intermediate2:00remaining
Ruby block variable behavior
What will this Ruby code output?
Ruby
x = 10 [1, 2, 3].each do |x| puts x end puts x
Attempts:
2 left
💡 Hint
Consider if the block variable
x affects the outer x.✗ Incorrect
The block variable x is a new variable local to the block and does not overwrite the outer x. So the last puts x prints 10.
🔧 Debug
advanced2:00remaining
Identify the runtime error in Ruby code
What error does this Ruby code raise when run?
Ruby
def divide(a, b) a / b end puts divide(10, 0)
Attempts:
2 left
💡 Hint
What happens when you divide by zero in Ruby?
✗ Incorrect
Dividing by zero in Ruby raises a ZeroDivisionError.
🧠 Conceptual
advanced2:00remaining
Ruby constant lookup at runtime
Given this Ruby code, what will be printed when run?
Ruby
module A X = 100 class B X = 200 def self.show puts X end end end A::B.show
Attempts:
2 left
💡 Hint
Constants inside classes override outer module constants with the same name.
✗ Incorrect
The constant X inside class B is 200, which is used by show. The outer A::X is not used here.
❓ Predict Output
expert2:00remaining
Ruby dynamic method definition and runtime behavior
What is the output of this Ruby code?
Ruby
class C define_method(:hello) do |name| "Hello, #{name}!" end end obj = C.new puts obj.hello("Bob")
Attempts:
2 left
💡 Hint
Look at how
define_method creates methods at runtime.✗ Incorrect
define_method dynamically defines an instance method hello that takes one argument and returns a greeting string.