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
Why did my proposals get rejected?
okuramasafumi
1
470
A suggestion for the future of RDoc
okuramasafumi
1
69
15 JSON serializers for Ruby
okuramasafumi
2
120
Tech Events, Should We See Them from the Outside or the Inside?
okuramasafumi
1
44
Creating gems 101
okuramasafumi
1
84
How NOT to make your DSL terrible
okuramasafumi
0
310
オブジェクトしこう
okuramasafumi
2
220
Debugging Alba
okuramasafumi
1
200
Learning Ruby
okuramasafumi
5
570
Other Decks in Programming
See All in Programming
責務を分離するための例外設計 - PHPカンファレンス 2024
kajitack
1
560
なまけものオバケたち -PHP 8.4 に入った新機能の紹介-
tanakahisateru
1
120
Асинхронность неизбежна: как мы проектировали сервис уведомлений
lamodatech
0
730
talk-with-local-llm-with-web-streams-api
kbaba1001
0
180
From Translations to Multi Dimension Entities
alexanderschranz
2
130
これが俺の”自分戦略” プロセスを楽しんでいこう! - Developers CAREER Boost 2024
niftycorp
PRO
0
190
PHPとAPI Platformで作る本格的なWeb APIアプリケーション(入門編) / phpcon 2024 Intro to API Platform
ttskch
0
170
「Chatwork」Android版アプリを 支える単体テストの現在
okuzawats
0
180
103 Early Hints
sugi_0000
1
230
range over funcの使い道と非同期N+1リゾルバーの夢 / about a range over func
mackee
0
110
LLM Supervised Fine-tuningの理論と実践
datanalyticslabo
4
1.1k
Symfony Mapper Component
soyuka
2
730
Featured
See All Featured
Fashionably flexible responsive web design (full day workshop)
malarkey
405
66k
How To Stay Up To Date on Web Technology
chriscoyier
789
250k
Imperfection Machines: The Place of Print at Facebook
scottboms
266
13k
Why Our Code Smells
bkeepers
PRO
335
57k
The Straight Up "How To Draw Better" Workshop
denniskardys
232
140k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
10
810
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
111
49k
4 Signs Your Business is Dying
shpigford
181
21k
Product Roadmaps are Hard
iamctodd
PRO
49
11k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
251
21k
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
Code Reviewing Like a Champion
maltzj
520
39k
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Λ ࡞ͬͯΈΑ͏ʂ
ΤσΟλ͕։ ͖·͢