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
RubyKaigi 2025: Class New, A New Approach
tenderlove
0
78
RubyKaigi Dev Meeting 2025
tenderlove
1
3.2k
Speeding up Instance Variables in Ruby 3.3
tenderlove
2
440
[Feature #20425] Speeding up delegate methods
tenderlove
3
280
RailsConf 2023
tenderlove
30
1.2k
Don't @ Me! Faster Instance Variables with Object Shapes
tenderlove
1
470
RailsConf 2022 Keynote
tenderlove
2
570
Some Assembly Required
tenderlove
1
580
HexDevs 2021
tenderlove
1
470
Other Decks in Programming
See All in Programming
Jakarta EE Meets AI
ivargrimstad
0
580
バイブスあるコーディングで ~PHP~ 便利ツールをつくるプラクティス
uzulla
1
320
Advanced Micro Frontends: Multi Version/ Framework Scenarios
manfredsteyer
PRO
0
150
実践!App Intents対応
yuukiw00w
0
120
Google I/O Extended Incheon 2025 ~ What's new in Android development tools
pluu
1
220
新しいモバイルアプリ勉強会(仮)について
uetyo
1
250
副作用と戦う PHP リファクタリング ─ ドメインイベントでビジネスロジックを解きほぐす
kajitack
3
520
技術的負債で信頼性が限界だったWordPress運用をShifterで完全復活させた話
rvirus0817
0
160
AIに安心して任せるためにTypeScriptで一意な型を作ろう
arfes0e2b3c
0
330
Claude Code で Astro blog を Pages から Workers へ移行してみた
codehex
0
170
Android 15以上でPDFのテキスト検索を爆速開発!
tonionagauzzi
0
190
Bedrock AgentCore ObservabilityによるAIエージェントの運用
licux
8
560
Featured
See All Featured
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
10
1k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
44
2.4k
Making the Leap to Tech Lead
cromwellryan
134
9.5k
Product Roadmaps are Hard
iamctodd
PRO
54
11k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
656
60k
How to Think Like a Performance Engineer
csswizardry
25
1.8k
Become a Pro
speakerdeck
PRO
29
5.5k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
50
5.5k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
248
1.3M
We Have a Design System, Now What?
morganepeng
53
7.7k
Adopting Sorbet at Scale
ufuk
77
9.5k
Embracing the Ebb and Flow
colly
86
4.8k
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!