Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
What the hell is ActiveSupport::Concern?
Search
Aaron Patterson
December 04, 2012
Programming
5
280
What the hell is ActiveSupport::Concern?
A short talk about ActiveSupport::Concern.
Aaron Patterson
December 04, 2012
Tweet
Share
More Decks by Aaron Patterson
See All by Aaron Patterson
Speeding up Instance Variables in Ruby 3.3
tenderlove
2
380
[Feature #20425] Speeding up delegate methods
tenderlove
3
240
RailsConf 2023
tenderlove
29
1k
Don't @ Me! Faster Instance Variables with Object Shapes
tenderlove
1
430
RailsConf 2022 Keynote
tenderlove
2
530
Some Assembly Required
tenderlove
1
540
HexDevs 2021
tenderlove
1
440
Compacting GC for MRI
tenderlove
60
4.5k
But At What Cost?
tenderlove
9
15k
Other Decks in Programming
See All in Programming
AI Coding Agent Enablement - エージェントを自走させよう
yukukotani
12
4k
AtCoder Heuristic First-step Vol.1 講義スライド
terryu16
3
1.1k
フロントエンドテストの育て方
quramy
11
2.8k
Vibe Codingをせずに Clineを使っている
watany
10
4.1k
PsySHから紐解くREPLの仕組み
muno92
PRO
1
530
php-fpm がリクエスト処理する仕組みを追う / Tracing-How-php-fpm-Handles-Requests
shin1x1
5
890
Kubernetesで実現できるPlatform Engineering の現在地
nwiizo
3
1.8k
AHC 044 混合整数計画ソルバー解法
kiri8128
0
320
AI時代のプログラミング教育 / programming education in ai era
kishida
23
21k
複数ドメインに散らばってしまった画像…! 運用中のPHPアプリに後からCDNを導入する…!
suguruooki
0
450
AI Agents with JavaScript
slobodan
0
190
Denoでフロントエンド開発 2025年春版 / Frontend Development with Deno (Spring 2025)
petamoriken
1
1.3k
Featured
See All Featured
Side Projects
sachag
452
42k
Building Better People: How to give real-time feedback that sticks.
wjessup
367
19k
Producing Creativity
orderedlist
PRO
344
40k
Raft: Consensus for Rubyists
vanstee
137
6.9k
Speed Design
sergeychernyshev
28
870
Unsuck your backbone
ammeep
670
57k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
34
2.9k
Testing 201, or: Great Expectations
jmmastey
42
7.4k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
45
9.5k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
28
9.4k
Docker and Python
trallard
44
3.3k
Become a Pro
speakerdeck
PRO
27
5.2k
Transcript
Thanks!
Hark
Substantial
What the hell is ActiveSupport::Concern?
I have no idea.
U Concerned, Bro?
Aaron Patterson @tenderlove
Lead Pun Architect
Substantial, Thanks
None
Gorbachev Puff-Puff Thunderhorse
None
None
None
None
None
None
Rails Core ~ 3 years (I think)
Confession
What’s in a name?
Classes ❤ File ❤ Hash ❤ Set ❤ Array
Modules ❤ Enumerable ❤ Comparable
AS::Concern
“Concern” is a noun
“Concern” is a noun
“Son, We’re Concerned”
Grown Man.
Stay Classy Change Your Self Track Dependents
Stay Classy.
Class Methods module MyEnumerable def my_first end module ClassMethods def
my_new end end def self.included(klass) klass.extend ClassMethods end end
Including class Aaron include MyEnumerable end p Aaron.respond_to?(:my_new) # =>
true p Aaron.new.respond_to?(:my_first) # => true
With Concern module MyEnumerable extend ActiveSupport::Concern def my_first end module
ClassMethods def my_new end end end
Including class Aaron include MyEnumerable end p Aaron.respond_to?(:my_new) # =>
true p Aaron.new.respond_to?(:my_first) # => true
Be Careful!
Normal Include module MyEnumerable def my_first end end class Aaron
p singleton_methods # => [] include MyEnumerable p singleton_methods # => [] end
Normal Extend module MyEnumerable def my_first end end class Aaron
p singleton_methods # => [] extend MyEnumerable p singleton_methods # => [:my_first] end
Users May Be Surprised
Do I need this?
Are you coupled?
Change Your Self.
Setting up ivars
Class Accessor module MyEnumerable module ClassMethods attr_accessor :foo end def
self.included klass klass.extend ClassMethods end end
Warning! class Aaron include MyEnumerable p foo end
Warning! class Aaron include MyEnumerable p foo end @foo not
initialized
Class Accessor module MyEnumerable module ClassMethods attr_accessor :foo end def
self.included klass klass.extend ClassMethods @foo = 10 end end
Class Accessor module MyEnumerable module ClassMethods attr_accessor :foo end def
self.included klass klass.extend ClassMethods @foo = 10 end end self == MyEnumerable
Class Accessor module MyEnumerable module ClassMethods attr_accessor :foo end def
self.included klass klass.extend ClassMethods klass.class_eval do @foo = 10 end end end
Working Code class Aaron include MyEnumerable p foo # =>
10 end
With Concern module MyEnumerable extend ActiveSupport::Concern module ClassMethods attr_accessor :foo
end included do @foo = 10 end end self == included class
Isolating Setup
Class Method Calls class Foo < ActiveRecord::Base scope :foo, ->
{ ... } scope :bar, -> { ... } scope :baz, -> { ... } # other methods end
Module Extraction module Findable def self.included(klass) klass.scope :foo, -> {
... } klass.scope :bar, -> { ... } klass.scope :baz, -> { ... } end end
Module Extraction module Findable def self.included(klass) klass.scope :foo, -> {
... } klass.scope :bar, -> { ... } klass.scope :baz, -> { ... } end end
class_eval module Findable def self.included(klass) klass.class_eval do scope :foo, ->
{ ... } scope :bar, -> { ... } scope :baz, -> { ... } end end end
Concerned module Findable extend ActiveSupport::Concern included do scope :foo, ->
{ ... } scope :bar, -> { ... } scope :baz, -> { ... } end end
Track Your Dependents.
Two Modules module Foo module ClassMethods def some_method end #
.... end end module Bar def self.included klass klass.some_method end end
Bar depends on Foo
Usage class Aaron include Foo include Bar end
Usage - BOOM! class Aaron include Bar include Foo end
Try to Fix module Foo module ClassMethods def some_method end
end end module Bar include Foo def self.included klass klass.some_method end end
Try to Fix module Foo module ClassMethods def some_method end
end end module Bar include Foo def self.included klass klass.some_method end end
Fix with class_eval module Bar def self.included klass klass.class_eval do
include Foo end klass.some_method end end
Concerned module Bar extend ActiveSupport::Concern include Foo included do some_method
end end
Concerned module Bar extend ActiveSupport::Concern include Foo included do some_method
end end NOT Ruby’s Include
When should I be concerned?
You probably shouldn’t be.
Can you simplify?
Many Dependencies
THANKS!