Challenge - 5 Problems
Ruby Namespacing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of nested module constant access
What is the output of this Ruby code?
Ruby
module Outer module Inner VALUE = 42 end end puts Outer::Inner::VALUE
Attempts:
2 left
💡 Hint
Think about how constants are accessed inside nested modules.
✗ Incorrect
The constant VALUE is defined inside Inner module which is nested inside Outer. Accessing it with Outer::Inner::VALUE returns 42.
❓ Predict Output
intermediate2:00remaining
Method call with same name in different modules
What will this Ruby code print?
Ruby
module A def self.greet 'Hello from A' end end module B def self.greet 'Hello from B' end end puts A.greet puts B.greet
Attempts:
2 left
💡 Hint
Each module has its own greet method.
✗ Incorrect
Each module defines its own greet method. Calling A.greet prints 'Hello from A' and B.greet prints 'Hello from B'.
🔧 Debug
advanced2:00remaining
Why does this code raise an error?
This code raises an error. What is the cause?
Ruby
module Outer module Inner def self.message 'Hi' end end end puts Inner.message
Attempts:
2 left
💡 Hint
Check how you access nested modules outside their parent.
✗ Incorrect
Inner is defined inside Outer, so you must access it as Outer::Inner. Using Inner alone causes a NameError.
📝 Syntax
advanced2:00remaining
Identify the syntax error in module nesting
Which option contains the correct syntax to define a nested module in Ruby?
Attempts:
2 left
💡 Hint
Remember Ruby uses 'module' and 'end' keywords with semicolons allowed.
✗ Incorrect
Option B uses correct Ruby syntax with semicolons to separate statements. Others use invalid syntax like braces or colons.
🚀 Application
expert2:00remaining
How many constants are accessible from Outer module?
Given this code, how many constants can be accessed directly using Outer::CONST_NAME?
Ruby
module Outer CONST1 = 1 module Inner CONST2 = 2 end CONST3 = 3 end
Attempts:
2 left
💡 Hint
Only constants defined directly inside Outer are accessible as Outer::CONST_NAME.
✗ Incorrect
CONST1 and CONST3 are defined directly inside Outer and accessible as Outer::CONST1 and Outer::CONST3. CONST2 is inside Inner and must be accessed as Outer::Inner::CONST2.