💎 Ruby on Rails
Advanced
What is the difference between include, extend, and prepend in Ruby?
Answer
include mixes a module's methods into the class as instance methods and inserts the module into the class's ancestor chain after the class. extend adds a module's methods as class methods (extends the singleton class). prepend inserts the module before the class in the ancestor chain, so the module's methods are called first — allowing you to wrap or override methods without aliasing. Rails' ActiveSupport::Concern uses prepend internally. Example: prepend is used to add instrumentation around existing methods by calling super from within the module method.
Previous
What is metaprogramming in Ruby/Rails and give examples?
Next
How does Rails handle database connection pooling?