prepend and include in Ruby

In Ruby, the prepend keyword is used to mix a module into a class or an object, in a way that the methods, constants, or variables of the module are inserted before the methods, constants, or variables of the class or object itself.

The prepend keyword is similar to the include keyword, which is used to mix a module into a class or object. However, while include mixes the module after the class or object, prepend mixes the module before the class or object, which means that the methods, constants, or variables of the module will have precedence over the methods, constants, or variables of the class or object.

For example, consider the following code, which defines a Logger module, and which mixes the module into the User class using the include and prepend keywords:

module Logger
  def log(message)
    puts message
  end
end

class User
  include Logger
  prepend Logger

  def initialize(name)
    @name = name
  end

  def log(message)
    puts "User: #{message}"
  end

  def greet
    log "Hello, #{@name}!"
  end
end

user = User.new("John")
user.greet
# => User: Hello, John!
# => Hello, John!

In this example, the Logger module defines a log method, which outputs a message to the standard output. The User class includes and prepends the Logger module, which means that it mixes the log method of the module before and after the log method of the class. When the greet method is called, the log method of the Logger module is called first, and it overrides the log method of the User class, which is called second.

The prepend keyword is useful when you want to mix a module into a class or object, and you want to ensure that the methods, constants, or variables of the module take precedence over the methods, constants, or variables of the class or object. This can be useful to override or extend the behavior of the class or object, or to provide additional or specialized functionality. However, you should use prepend with caution, as it can cause conflicts or confusion if not used properly.

Did you find this article valuable?

Support Matheus Puppe Blog by becoming a sponsor. Any amount is appreciated!