0
0
Rubyprogramming~20 mins

Namespacing with modules in Ruby - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Ruby Namespacing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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
A42
Bnil
CNameError
DOuter::Inner::VALUE
Attempts:
2 left
💡 Hint
Think about how constants are accessed inside nested modules.
Predict Output
intermediate
2: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
A
Hello from A
Hello from A
B
Hello from B
Hello from A
C
Hello from A
Hello from B
DNoMethodError
Attempts:
2 left
💡 Hint
Each module has its own greet method.
🔧 Debug
advanced
2: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
ATypeError - because Inner is not a module
BNoMethodError: undefined method 'message' - because message is not defined
CSyntaxError - because module nesting is incorrect
DNameError: uninitialized constant Inner - because Inner is not accessible without Outer namespace
Attempts:
2 left
💡 Hint
Check how you access nested modules outside their parent.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in module nesting
Which option contains the correct syntax to define a nested module in Ruby?
Amodule Outer module Inner end end
Bmodule Outer; module Inner; end; end
Cmodule Outer { module Inner } end
Dmodule Outer: module Inner: end end
Attempts:
2 left
💡 Hint
Remember Ruby uses 'module' and 'end' keywords with semicolons allowed.
🚀 Application
expert
2: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
A2
B3
C1
D0
Attempts:
2 left
💡 Hint
Only constants defined directly inside Outer are accessible as Outer::CONST_NAME.