0
0
Rubyprogramming~10 mins

Namespacing with modules in Ruby - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a module named MathTools.

Ruby
module [1]
  def self.square(x)
    x * x
  end
end
Drag options to blanks, or click blank then click option'
ATools
BMathTools
CToolsMath
DMathTool
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase or incorrect module names.
Forgetting to capitalize module names.
2fill in blank
medium

Complete the code to call the square method inside the MathTools module.

Ruby
result = MathTools.[1](5)
puts result
Drag options to blanks, or click blank then click option'
Asquare
Bsquare_root
Csquared
Dpower
Attempts:
3 left
💡 Hint
Common Mistakes
Using method names that do not exist in the module.
Calling methods without the module prefix.
3fill in blank
hard

Fix the error in the code by completing the module namespace correctly.

Ruby
module Geometry
  module [1]
    def self.area(radius)
      3.14 * radius * radius
    end
  end
end
Drag options to blanks, or click blank then click option'
AShape
Bshapes
CShapes
Dshape
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase module names.
Using singular form when plural is more appropriate.
4fill in blank
hard

Fill both blanks to define a nested module and call its method correctly.

Ruby
module [1]
  module Calculator
    def self.add(a, b)
      a + b
    end
  end
end

result = [2]::Calculator.add(3, 4)
puts result
Drag options to blanks, or click blank then click option'
AMathTools
CToolsMath
Attempts:
3 left
💡 Hint
Common Mistakes
Using different module names in definition and call.
Using dot notation instead of scope resolution operator.
5fill in blank
hard

Fill all three blanks to create a nested module with a method and call it properly.

Ruby
module [1]
  module [2]
    def self.greet(name)
      "Hello, #{name}!"
    end
  end
end

message = [3]::Greeter.greet("Alice")
puts message
Drag options to blanks, or click blank then click option'
AMyApp
BGreeter
DYourApp
Attempts:
3 left
💡 Hint
Common Mistakes
Mismatching module names between definition and call.
Using lowercase module names.