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
480
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
Symfony Mapper Component
soyuka
2
730
Fibonacci Function Gallery - Part 1
philipschwarz
PRO
0
220
tidymodelsによるtidyな生存時間解析 / Japan.R2024
dropout009
1
790
ゆるやかにgolangci-lintのルールを強くする / Kyoto.go #56
utgwkk
2
390
php-conference-japan-2024
tasuku43
0
310
なまけものオバケたち -PHP 8.4 に入った新機能の紹介-
tanakahisateru
1
120
バグを見つけた?それAppleに直してもらおう!
uetyo
0
180
useSyncExternalStoreを使いまくる
ssssota
6
1.1k
Cloudflare MCP ServerでClaude Desktop からWeb APIを構築
kutakutat
1
550
「Chatwork」Android版アプリを 支える単体テストの現在
okuzawats
0
180
Spatial Rendering for Apple Vision Pro
warrenm
0
110
return文におけるstd::moveについて
onihusube
1
1.1k
Featured
See All Featured
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
48
2.2k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
191
16k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
45
2.2k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
28
900
Building Applications with DynamoDB
mza
91
6.1k
Designing Experiences People Love
moore
138
23k
Designing for humans not robots
tammielis
250
25k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
665
120k
Code Reviewing Like a Champion
maltzj
520
39k
Building a Modern Day E-commerce SEO Strategy
aleyda
38
7k
Learning to Love Humans: Emotional Interface Design
aarron
273
40k
The Cost Of JavaScript in 2023
addyosmani
45
7k
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Λ ࡞ͬͯΈΑ͏ʂ
ΤσΟλ͕։ ͖·͢