0
0
Rubyprogramming~10 mins

Included hook 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 with an included hook method.

Ruby
module Greetings
  def self.[1](base)
    puts "Module included in #{base}"
  end
end
Drag options to blanks, or click blank then click option'
Aincluded
Bextend
Cinclude
Dinitialize
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'include' instead of 'included' for the hook method.
Defining the hook as an instance method instead of a class method.
2fill in blank
medium

Complete the code to include the module and trigger the included hook.

Ruby
class Person
  [1] Greetings
end
Drag options to blanks, or click blank then click option'
Aextend
Binclude
Cprepend
Dusing
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'extend' which adds methods as class methods instead of instance methods.
Using 'prepend' which changes method lookup order.
3fill in blank
hard

Fix the error in the included hook method definition.

Ruby
module Logger
  def self.included(base)
    base.[1] LoggerMethods
  end
end
Drag options to blanks, or click blank then click option'
Ainclude
Busing
Cextend
Dprepend
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'include' instead of 'extend' inside the included hook.
Not calling any method on base, causing no effect.
4fill in blank
hard

Fill both blanks to define a module with an included hook that adds class methods.

Ruby
module Trackable
  def self.[1](base)
    base.[2] ClassMethods
  end

  module ClassMethods
    def track
      puts "Tracking enabled"
    end
  end
end
Drag options to blanks, or click blank then click option'
Aincluded
Binclude
Cextend
Dprepend
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'include' instead of 'extend' to add class methods.
Misnaming the hook method.
5fill in blank
hard

Fill all three blanks to create a module with an included hook that adds instance and class methods.

Ruby
module Auditable
  def self.[1](base)
    base.[2] InstanceMethods
    base.[3] ClassMethods
  end

  module InstanceMethods
    def audit
      puts "Instance audit"
    end
  end

  module ClassMethods
    def audit
      puts "Class audit"
    end
  end
end
Drag options to blanks, or click blank then click option'
Aincluded
Binclude
Cextend
Dprepend
Attempts:
3 left
💡 Hint
Common Mistakes
Using extend for instance methods or include for class methods.
Forgetting to define the hook as a class method.