0
0
Rubyprogramming~10 mins

Extend for class 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 can be used as a class method.

Ruby
module Greetings
  def [1](name)
    "Hello, #{name}!"
  end
end
Drag options to blanks, or click blank then click option'
Amodule.say_hello
Bself.say_hello
Csay_hello
Ddef say_hello
Attempts:
3 left
💡 Hint
Common Mistakes
Adding 'self.' inside the module method definition.
2fill in blank
medium

Complete the code to extend the module as class methods in the class.

Ruby
class Person
  extend [1]
end
Drag options to blanks, or click blank then click option'
AGreetings
Bself.Greetings
Cinclude Greetings
Dmodule Greetings
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'include' instead of 'extend'.
3fill in blank
hard

Fix the error in calling the class method from the module.

Ruby
puts Person.[1]("Alice")
Drag options to blanks, or click blank then click option'
AsayHello
BGreetings.say_hello
Csay_hello
Dself.say_hello
Attempts:
3 left
💡 Hint
Common Mistakes
Calling the method with module prefix or wrong method name.
4fill in blank
hard

Fill both blanks to define and extend a module with a class method.

Ruby
module Logger
  def [1](msg)
    puts "Log: #{msg}"
  end
end

class App
  [2] Logger
end
Drag options to blanks, or click blank then click option'
Alog
Binclude
Cextend
Ddef
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'include' instead of 'extend' for class methods.
5fill in blank
hard

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

Ruby
module Utils
  def [1](value)
    value * 2
  end
end

class Calculator
  [2] Utils
end

result = Calculator.[3](5)
puts result
Drag options to blanks, or click blank then click option'
Adouble
Bextend
Dinclude
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'include' instead of 'extend' for class methods.