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
Writing simple and easy DSL with Ruby
Search
Masafumi Okura
January 29, 2022
Programming
2
180
Writing simple and easy DSL with Ruby
https://toyama-eng.connpass.com/event/233459/
Masafumi Okura
January 29, 2022
Tweet
Share
More Decks by Masafumi Okura
See All by Masafumi Okura
Alba: Why, How and What's So Interesting
okuramasafumi
0
210
Why did my proposals get rejected?
okuramasafumi
1
510
A suggestion for the future of RDoc
okuramasafumi
1
150
15 JSON serializers for Ruby
okuramasafumi
2
200
Tech Events, Should We See Them from the Outside or the Inside?
okuramasafumi
1
47
Creating gems 101
okuramasafumi
1
94
How NOT to make your DSL terrible
okuramasafumi
0
390
オブジェクトしこう
okuramasafumi
2
300
Debugging Alba
okuramasafumi
1
210
Other Decks in Programming
See All in Programming
生成AIでGitHubソースコード取得して仕様書を作成
shukob
0
630
Fixstars高速化コンテスト2024準優勝解法
eijirou
0
190
DevinとCursorから学ぶAIエージェントメモリーの設計とMoatの考え方
itarutomy
0
150
Androidアプリのモジュール分割における:x:commonを考える
okuzawats
1
280
HTML/CSS超絶浅い説明
yuki0329
0
190
php-conference-japan-2024
tasuku43
0
430
情報漏洩させないための設計
kubotak
5
1.3k
QA環境で誰でも自由自在に現在時刻を操って検証できるようにした話
kalibora
1
140
令和7年版 あなたが使ってよいフロントエンド機能とは
mugi_uno
10
5.2k
Lookerは可視化だけじゃない。UIコンポーネントもあるんだ!
ymd65536
1
130
BEエンジニアがFEの業務をできるようになるまでにやったこと
yoshida_ryushin
0
200
良いユニットテストを書こう
mototakatsu
11
3.6k
Featured
See All Featured
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
3.6k
Unsuck your backbone
ammeep
669
57k
Side Projects
sachag
452
42k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
33
2.7k
A Modern Web Designer's Workflow
chriscoyier
693
190k
No one is an island. Learnings from fostering a developers community.
thoeni
19
3.1k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
38
1.9k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
507
140k
GraphQLの誤解/rethinking-graphql
sonatard
68
10k
Keith and Marios Guide to Fast Websites
keithpitt
410
22k
Large-scale JavaScript Application Architecture
addyosmani
510
110k
Embracing the Ebb and Flow
colly
84
4.5k
Transcript
RubyͰ࡞ΔSimpleͰEasyͳ DSL Burikaigi 2022 ϧʔϜϗλϧΠΧ OKURA Masafumi, 2022
Ruby DSL
DSLͱʁ
None
Ҏ߱ͷ”DSL”ʮݴޠDSLʯΛࢦ͢ ͜ͱͱ͠·͢
RubyͰॻ͔ΕͨDSLͷྫʢRubyͰͷར༻ʣ • Rake • RSpec • FactoryBot • Sinatra •
Rails • ίʔϧόοΫɺόϦσʔγϣϯͳͲ
RSpecͷྫʢREADMEΑΓʣ
RubyͰॻ͔ΕͨDSLͷྫʢRuby֎Ͱͷར༻ʣ • Homebrew • Chef • Vagrant • Capistrano
HomebrewͷྫʢZoomͷCaskΑΓʣ
ݴޠDSLͷϝϦοτ • ϓϩάϥϛϯάݴޠͷϑϧػೳ͕͑Δ • มؔ • ಡΉ͚ͩͳΒϓϩάϥϛϯάݴޠͷ͕ࣝෆཁ • ϓϩάϥϛϯάݴޠͱઃఆϑΝΠϧͷ͍͍ͱ͜औΓ
ࣗݾհ • ໊લɿେխ࢙ • ॴଐɿϑϦʔϥϯε • ՝֎׆ಈɿKaigi on RailsνʔϑΦʔΨφΠβʔɺGrow.rb/Entaku.rb ΦʔΨφΠβʔɺAlba࡞ऀ
• ͖ͳRubyͷϝιουɿinstance_evalʢ2લͷൃදΛࢀরʣ
Alba • JSONγϦΞϥΠβʢҙͷΦϒδΣΫτΛJSONʹมʣ • ͍ʢྨࣅͷgemͱൺͯ2൪ʹ͍ʣ • 300ms͔͔͍ͬͯͨAPIΛ100ms·ͰߴԽͨ͠Γ • Ϣʔβʔ͕DSLΛॻ͘ͱAlba͕ͦΕʹैͬͯΦϒδΣΫτ͔ΒଐੑΛऔಘ ͯ͠JSONΛߏங͢Δ
• ৄࡉ https://github.com/okuramasafumi/alba Λࢀর
ࠓͷൃද ͷߏ
ೋ෦ߏ • ୈҰ෦ɿDSLʹؔ͢Δཧతߟ • RubyͱDSLɺϑΣʔζͷͳͲ • ୈೋ෦ɿDSLΛ࡞ͬͯΈΔ • ؆୯ͳDSLΛ࡞ͬͯΈΔ •
࣮͕SimpleͰ͍ํ͕EasyͳDSLΛ࡞Δ • λΠτϧճऩ
ୈҰ෦ɿ DSLʹ͍ͭͯͷ ߟ
ͳͥRubyͰ DSLΛॻ͘ͷ͔
RubyͰDSLΛॻ͘ཧ༝ • τοϓϨϕϧʢRubyͰ”main”ʣʹϝιουΛఆ͍ٛ͢͠ • ϝιουͷׅހ͕লུͰ͖ΔͷͰݟ͕ͨӳจͬΆ͘ͳΓ͍͢ • => ಡΈ͍͢ • ωετͨ͠ϒϩοΫͱ”eval”ϝιουͷΈ߹ΘͤΛ͏͜ͱͰෳ
ࡶͳϩδοΫදݱͰ͖Δ • => ॻ͖͍͢
ٯʹݴ͏ͱ • DSL୯ͳΔϝιουʢಛʹΫϥεϝιουʣ • evalͱϒϩοΫͷ͍ํ͑͞Θ͔ΕDSL࡞ΕΔʂ
Rubyʹ͓͚ΔDSLͷྨʹ͍ͭͯ • “main”ύλʔϯɿ”main”ʹϝιουΛఆٛ͢Δύλʔϯ • Rake, SinatraͳͲ • “include”ύλʔϯɿϞδϡʔϧʹσʔλͱϩδοΫΛू͠ɺ`include``extend`͠ ͨΫϥε͕ͦΕʹΞΫηεͰ͖Δύλʔϯ •
Railsͷଟ͘ͷDSL • ಠཱύλʔϯɿϞδϡʔϧΛ`include`͠ͳͯ͘DSLΛ͑Δ • RSpec, FactoryBotͳͲ
DSLͷ্࣮ͷίπ • ʮఆٛɾొʯʮࢦఆʯʮ࣮ߦʯͷϑΣʔζ͕͔Ε͍ͯΔͨΊɺ͜ ΕΒΛ͔ͬ͠Γ্ͭͭ͠खʹ݁߹͢Δ • ʮఆٛɾొʯϑΣʔζͰʮ࣮ߦʯϑΣʔζͰߦ͍͍ͨ͜ͱΛొ ͍ͯ͘͠ • ʮࢦఆʯϑΣʔζͰʮఆٛɾొʯ͞Εͨͷ͔Βࠓճద༻͍ͨ͠ ͷΛࢦఆ͢Δ
• ʮ࣮ߦʯϑΣʔζͰʮࢦఆʯ͞ΕͨͷΛ࣮ߦ͢Δ
ݱ࣮ͷDSLʹ͓͚ΔϑΣʔζͷ ఆٛ ࢦఆ ࣮ߦ 3BLF UBTL EFGBVMUͳͲ SBLFίϚϯυ 34QFD EFTDSJCF
JUͳͲ STQFDίϚϯυ 3BJMTόϦσʔγϣϯ A"DUJWF.PEFM7BMJEBUPSAΫ ϥεΛܧঝͨ͠Ϋϥεͷ࡞ WBMJEBUFTϝιου WBMJE ϝιου "MCB A"MCB3FTPVSDFAϞδϡʔϧ ΛAJODMVEFA͔ͯ͠Β ABUUSJCVUFTAͳͲ Ϋϥεબ TFSJBMJ[Fϝιου
ࠓճ࡞ΔDSLʹ͍ͭͯ • Γ͍ͨ͜ͱɿRailsͷόϦσʔγϣϯͷ؆қ൛ • ࣮ɿϞδϡʔϧΛ`include`͢ΔͱɺΫϥεϝιουͷ`.validates`ͱ Πϯελϯεϝιουͷ`#validate`͕ར༻ՄೳʹͳΔ • `.validates`ʹόϦσʔλͷ໊લͱΦϓγϣϯΛ͢ɺ͢ΔͱόϦ σʔλ͕Ϋϥεʹొ͞ΕΔ •
`#validate`ࣗͷΫϥεʹొ͞ΕͨόϦσʔλΛ࣮ߦ͢Δ
ʢଓʣࠓճ࡞ΔDSLʹ͍ͭͯ • ॳظঢ়ଶͰόϦσʔλ͕ଘࡏ͠ͳ͍ͨΊɺ`.validator`ϝιουͰఆ ͍ٛͯ͘͠ • ޙͰ`.validates`Ͱࢦఆ͢ΔͨΊͷ໊લͱɺϩδοΫʢόϦσʔγϣ ϯͷ༰ʣΛఆٛ͢Δ • ϩδοΫϒϩοΫͰఆٛ͢Δɺ͜ͷͱ͖ରͷΦϒδΣΫτͱඞ ཁͳʢ͞ΛόϦσʔτ͢ΔͳΒͦͷࣈʣΛड͚औΔ
͓·͚ͷߟ • ϓϩάϥϛϯάͰʮߏʯͱʮৼΔ͍ʯͷ2͕ͭॏཁ • cf. σβΠϯύλʔϯͷྨ • ʮఆٛɾొʯͱʮࢦఆʯߏͰʮ࣮ߦʯৼΔ͍ • ߏ੩తͰৼΔ͍ಈతɺͱ͍͑Δ
• DSLΛ͏ͱߏͱৼΔ͍ΛࣗવʹͰ͖ͯݟ௨͕͠ྑ͘ͳΔ
ୈೋ෦ɿ ࣮ࡍʹDSLΛ ࡞ͬͯΈΑ͏ʂ
ΤσΟλ͕։ ͖·͢