O'Reilly logo

Metaprogramming Ruby 2 by Paolo Perrotta

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

ActiveSupport::Concern

ActiveSupport::Concern encapsulates the include-and-extend trick and fixes the problem of chained inclusions. A module can get this functionality by extending Concern and defining its own ClassMethods module:

part2/using_concern.rb
 
require ​'active_support'
 
 
module​ MyConcern
 
extend ActiveSupport::Concern
 
 
def​ an_instance_method; ​"an instance method"​; ​end
 
 
module​ ClassMethods
 
def​ a_class_method; ​"a class method"​; ​end
 
end
 
end
 
 
class​ MyClass
 
include MyConcern
 
end
 
 
MyClass.new.an_instance_method ​# => "an instance method"
 
MyClass.a_class_method ​# => "a class method"

In the rest of this chapter I’ll use the word “concern” with a lowercase C to mean “a module that extend

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required