Challenge - 5 Problems
Ruby Module Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of module method call
What is the output of this Ruby code?
Ruby
module Greetings def self.say_hello "Hello from module!" end end puts Greetings.say_hello
Attempts:
2 left
💡 Hint
Look at how the method is defined with self inside the module.
✗ Incorrect
The method say_hello is defined as a module method using self. Calling Greetings.say_hello prints the string.
❓ Predict Output
intermediate2:00remaining
Output of including a module
What will be printed when this Ruby code runs?
Ruby
module Talk def speak "I can talk" end end class Person include Talk end puts Person.new.speak
Attempts:
2 left
💡 Hint
Including a module adds its methods as instance methods.
✗ Incorrect
The module Talk is included in Person, so Person instances can call speak, printing 'I can talk'.
❓ Predict Output
advanced2:00remaining
Output of nested module constants
What is the output of this Ruby code?
Ruby
module Outer VALUE = 10 module Inner VALUE = 20 def self.value VALUE end end end puts Outer::Inner.value
Attempts:
2 left
💡 Hint
Constants inside nested modules are accessed by their own scope.
✗ Incorrect
Inner module has its own VALUE constant set to 20. The method returns Inner::VALUE, which is 20.
❓ Predict Output
advanced2:00remaining
Error raised by invalid module syntax
What error does this Ruby code raise?
Ruby
module Sample def method puts "Hello" # missing end here
Attempts:
2 left
💡 Hint
Check if all blocks and modules are properly closed.
✗ Incorrect
The module and method definitions are not closed with 'end', causing a SyntaxError.
🧠 Conceptual
expert2:00remaining
Number of methods available after module inclusion
Given this Ruby code, how many methods can be called on an instance of class C?
Ruby
module M1 def a; 1; end end module M2 def b; 2; end end class C include M1 include M2 def c; 3; end end obj = C.new
Attempts:
2 left
💡 Hint
Including modules adds their instance methods to the class.
✗ Incorrect
Class C has method c and includes M1 and M2, adding methods a and b. So obj has 3 methods: a, b, c.