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
リアーキテクチャxDDD 1年間の取り組みと進化
hsawaji
1
220
Contemporary Test Cases
maaretp
0
130
ローコードSaaSのUXを向上させるためのTypeScript
taro28
1
610
とにかくAWS GameDay!AWSは世界の共通言語! / Anyway, AWS GameDay! AWS is the world's lingua franca!
seike460
PRO
1
860
最新TCAキャッチアップ
0si43
0
140
タクシーアプリ『GO』のリアルタイムデータ分析基盤における機械学習サービスの活用
mot_techtalk
4
1.4k
Tauriでネイティブアプリを作りたい
tsucchinoko
0
370
C++でシェーダを書く
fadis
6
4.1k
Make Impossible States Impossibleを 意識してReactのPropsを設計しよう
ikumatadokoro
0
170
Compose 1.7のTextFieldはPOBox Plusで日本語変換できない
tomoya0x00
0
190
シェーダーで魅せるMapLibreの動的ラスタータイル
satoshi7190
1
480
What’s New in Compose Multiplatform - A Live Tour (droidcon London 2024)
zsmb
1
470
Featured
See All Featured
Art, The Web, and Tiny UX
lynnandtonic
297
20k
The Pragmatic Product Professional
lauravandoore
31
6.3k
A better future with KSS
kneath
238
17k
The Power of CSS Pseudo Elements
geoffreycrofte
73
5.3k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
250
21k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
25
1.8k
Building Adaptive Systems
keathley
38
2.3k
Imperfection Machines: The Place of Print at Facebook
scottboms
265
13k
Scaling GitHub
holman
458
140k
A Philosophy of Restraint
colly
203
16k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
47
5k
Building a Scalable Design System with Sketch
lauravandoore
459
33k
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Λ ࡞ͬͯΈΑ͏ʂ
ΤσΟλ͕։ ͖·͢