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
170
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
440
A suggestion for the future of RDoc
okuramasafumi
1
66
15 JSON serializers for Ruby
okuramasafumi
2
110
Tech Events, Should We See Them from the Outside or the Inside?
okuramasafumi
1
39
Creating gems 101
okuramasafumi
1
82
How NOT to make your DSL terrible
okuramasafumi
0
280
オブジェクトしこう
okuramasafumi
2
210
Debugging Alba
okuramasafumi
1
190
Learning Ruby
okuramasafumi
5
560
Other Decks in Programming
See All in Programming
Snowflake x dbtで作るセキュアでアジャイルなデータ基盤
tsoshiro
2
520
RubyLSPのマルチバイト文字対応
notfounds
0
120
광고 소재 심사 과정에 AI를 도입하여 광고 서비스 생산성 향상시키기
kakao
PRO
0
170
TypeScript Graph でコードレビューの心理的障壁を乗り越える
ysk8hori
2
1.1k
ペアーズにおけるAmazon Bedrockを⽤いた障害対応⽀援 ⽣成AIツールの導⼊事例 @ 20241115配信AWSウェビナー登壇
fukubaka0825
6
2k
Pinia Colada が実現するスマートな非同期処理
naokihaba
4
230
WebフロントエンドにおけるGraphQL(あるいはバックエンドのAPI)との向き合い方 / #241106_plk_frontend
izumin5210
4
1.4k
[Do iOS '24] Ship your app on a Friday...and enjoy your weekend!
polpielladev
0
110
受け取る人から提供する人になるということ
little_rubyist
0
250
イベント駆動で成長して委員会
happymana
1
330
よくできたテンプレート言語として TypeScript + JSX を利用する試み / Using TypeScript + JSX outside of Web Frontend #TSKaigiKansai
izumin5210
6
1.7k
エンジニアとして関わる要件と仕様(公開用)
murabayashi
0
300
Featured
See All Featured
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
159
15k
Done Done
chrislema
181
16k
Why Our Code Smells
bkeepers
PRO
334
57k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
229
52k
The Power of CSS Pseudo Elements
geoffreycrofte
73
5.3k
Imperfection Machines: The Place of Print at Facebook
scottboms
265
13k
Gamification - CAS2011
davidbonilla
80
5k
Writing Fast Ruby
sferik
627
61k
Bootstrapping a Software Product
garrettdimon
PRO
305
110k
BBQ
matthewcrist
85
9.3k
Become a Pro
speakerdeck
PRO
25
5k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
250
21k
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Λ ࡞ͬͯΈΑ͏ʂ
ΤσΟλ͕։ ͖·͢