How do I include a Module into another Module (Refactor AASM code and custom states into Module)

You can’t include one module in another exactly, but you can tell a module to include other modules in the class into which it’s included:

module Bmodule
  def greet
    puts 'hello world'
  end
end

module Amodule
  def self.included klass
    klass.class_eval do
      include Bmodule
    end
  end
end

class MyClass
  include Amodule
end

MyClass.new.greet # => hello world

It’s best to only do this if Bmodule is actually data that is necessary for Amodule to function, otherwise it can lead to confusion because it’s not explicitly included in MyClass.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)