Complete the code to define a module method that returns a greeting string.
module Greeter def self.[1] "Hello!" end end puts Greeter.greet
The method name greet matches the call Greeter.greet, so it correctly defines the module method.
Complete the code to call the module method correctly.
module Calculator def self.square(x) x * x end end result = Calculator.[1](4) puts result
The method square is defined as a module method and called with Calculator.square(4).
Fix the error in the module method definition to make it a proper module method.
module Printer def [1](text) puts text end end Printer.print("Hello")
self. and expecting to call it on the module.To define a module method, prefix the method name with self.. This makes it callable on the module itself.
Fill both blanks to define and call a module method that returns the sum of two numbers.
module MathOps def [1](a, b) a [2] b end end puts MathOps.add(3, 4)
self..The method is defined as self.add to be a module method, and it returns the sum using the + operator.
Fill all three blanks to define a module method that checks if a number is even and call it.
module NumberCheck def [1](num) num [2] 2 == 0 end end puts NumberCheck.[3](10)
self..The method is defined as self.even? to be a module method. It uses the modulo operator % to check if the number is divisible by 2. The method is called as NumberCheck.even?(10).