0
0
Rubyprogramming~10 mins

Module methods 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 method that returns a greeting string.

Ruby
module Greeter
  def self.[1]
    "Hello!"
  end
end

puts Greeter.greet
Drag options to blanks, or click blank then click option'
Agreet
Bhello
Cgreeting
Dsay_hello
Attempts:
3 left
💡 Hint
Common Mistakes
Using a method name different from the one called on the module.
Defining an instance method instead of a module method.
2fill in blank
medium

Complete the code to call the module method correctly.

Ruby
module Calculator
  def self.square(x)
    x * x
  end
end

result = Calculator.[1](4)
puts result
Drag options to blanks, or click blank then click option'
Apower
Bsquare_root
Cmultiply
Dsquare
Attempts:
3 left
💡 Hint
Common Mistakes
Calling a method name that does not exist in the module.
Trying to call the method without the module name.
3fill in blank
hard

Fix the error in the module method definition to make it a proper module method.

Ruby
module Printer
  def [1](text)
    puts text
  end
end

Printer.print("Hello")
Drag options to blanks, or click blank then click option'
Aprint
Bself.print
CPrint
Dprinter
Attempts:
3 left
💡 Hint
Common Mistakes
Defining the method without self. and expecting to call it on the module.
Using incorrect capitalization for method names.
4fill in blank
hard

Fill both blanks to define and call a module method that returns the sum of two numbers.

Ruby
module MathOps
  def [1](a, b)
    a [2] b
  end
end

puts MathOps.add(3, 4)
Drag options to blanks, or click blank then click option'
Aself.add
Bsubtract
C+
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Defining the method without self..
Using the wrong operator for addition.
5fill in blank
hard

Fill all three blanks to define a module method that checks if a number is even and call it.

Ruby
module NumberCheck
  def [1](num)
    num [2] 2 == 0
  end
end

puts NumberCheck.[3](10)
Drag options to blanks, or click blank then click option'
Aself.even?
B%
Ceven?
Dis_even
Attempts:
3 left
💡 Hint
Common Mistakes
Defining the method without self..
Using the wrong operator instead of modulo.
Calling the method with a different name than defined.