0
0
Rubyprogramming~20 mins

Module declaration syntax in Ruby - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Ruby Module Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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
AHello from module!
BSyntaxError
CNoMethodError
Dnil
Attempts:
2 left
💡 Hint
Look at how the method is defined with self inside the module.
Predict Output
intermediate
2: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
ASyntaxError
BNoMethodError
CI can talk
Dnil
Attempts:
2 left
💡 Hint
Including a module adds its methods as instance methods.
Predict Output
advanced
2: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
Anil
B20
CNameError
D10
Attempts:
2 left
💡 Hint
Constants inside nested modules are accessed by their own scope.
Predict Output
advanced
2:00remaining
Error raised by invalid module syntax
What error does this Ruby code raise?
Ruby
module Sample
  def method
    puts "Hello"
  # missing end here
ASyntaxError
BRuntimeError
CNameError
DNoMethodError
Attempts:
2 left
💡 Hint
Check if all blocks and modules are properly closed.
🧠 Conceptual
expert
2: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
A0
B2
C1
D3
Attempts:
2 left
💡 Hint
Including modules adds their instance methods to the class.