0
0
Rubyprogramming~5 mins

Included hook in Ruby - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of the included hook in Ruby modules?
The <code>included</code> hook runs automatically when a module is included in a class or another module. It allows you to add extra behavior or setup at the moment of inclusion.
Click to reveal answer
beginner
How do you define an included hook inside a Ruby module?
You define a class method named <code>self.included(base)</code> inside the module. The <code>base</code> parameter is the class or module that includes this module.
Click to reveal answer
intermediate
What can you do inside the included hook method?
Inside <code>included</code>, you can add methods, extend the including class with class methods, or perform setup tasks like adding callbacks or validations.
Click to reveal answer
intermediate
Example: What will happen when this module is included?<br><pre>module Greetings
  def self.included(base)
    base.extend(ClassMethods)
  end

  module ClassMethods
    def say_hello
      "Hello!"
    end
  end
end

class Person
  include Greetings
end

Person.say_hello</pre>
The <code>included</code> hook extends the <code>Person</code> class with the <code>ClassMethods</code> module. So calling <code>Person.say_hello</code> will return <code>"Hello!"</code>.
Click to reveal answer
intermediate
Why is the included hook useful compared to just including a module?
It lets you run extra code exactly when the module is included, like adding class methods or setting up behavior, which you can't do by just including instance methods.
Click to reveal answer
What argument does the included hook method receive?
AThe class or module that includes the module
BThe name of the module being included
CNo arguments are passed
DThe instance of the including class
Which keyword is used to define the included hook inside a module?
Adef include_hook
Bdef included(base)
Cdef included!
Ddef self.included(base)
What is a common use of the included hook?
ATo define instance variables
BTo delete methods from the including class
CTo add class methods to the including class
DTo create new modules
What happens if you include a module with an included hook in a class?
AThe hook runs only when an instance is created
BThe hook runs immediately during inclusion
CThe hook never runs
DThe hook runs when the class is defined
Can the included hook be used to add instance methods to the including class?
ANo, instance methods are added by the module itself
BYes, but only if you call super
CYes, by defining methods inside the hook
DNo, it only adds class variables
Explain what the included hook does in Ruby modules and give an example of how it can be used.
Think about what happens when you include a module and want to add extra behavior.
You got /3 concepts.
    Describe the difference between adding instance methods directly in a module and using the included hook to add class methods.
    Consider what kinds of methods belong to instances vs the class itself.
    You got /3 concepts.