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
入門 名前
Search
Fujimura Daisuke
March 23, 2019
Programming
23
13k
入門 名前
Fujimura Daisuke
March 23, 2019
Tweet
Share
More Decks by Fujimura Daisuke
See All by Fujimura Daisuke
AIの時代で我々はどのようにコードを書くのか
fujimura
3
720
SaaSを作るという仕事について
fujimura
12
5.6k
一文字エイリアスのすすめ
fujimura
0
300
現役CTOが語る!RubyKaigiの楽しみ方
fujimura
0
1.2k
いかにして文系新卒エンジニアが「大きな問い」を大事にするCTOになったのか
fujimura
2
640
Kaigi on Rails 2022 - 既存Railsアプリ攻略法 CTOが見ること・やること・考えること
fujimura
14
3.8k
SimpleDelegator活用のご提案
fujimura
0
1.5k
それPostgreSQLでできるよ @ Rails Developer Meetup 2018 Day 1
fujimura
10
3.9k
ストリーム処理ライブラリはなぜ必要なのか
fujimura
2
620
Other Decks in Programming
See All in Programming
Go言語でターミナルフレンドリーなAIコマンド、afaを作った/fukuokago20_afa
monochromegane
2
140
推し活としてのrails new/oshikatsu_ha_iizo
sakahukamaki
3
1.1k
シールドクラスをはじめよう / Getting Started with Sealed Classes
mackey0225
3
350
組織に自動テストを書く文化を根付かせる戦略(2024秋版) / Building Automated Test Culture 2024 Autumn Edition
twada
PRO
10
4.3k
今日で分かる!カスタムコップの作り方
krpk1900
2
360
Boost Performance and Developer Productivity with Jakarta EE 11
ivargrimstad
0
440
Synchronizationを支える技術
s_shimotori
1
140
Progressive Web Apps für Desktop und Mobile mit Angular (Hands-on)
christianliebel
PRO
0
100
Kaigi on Rails 2024 - Rails APIモードのためのシンプルで効果的なCSRF対策 / kaigionrails-2024-csrf
corocn
4
2.8k
Snowflake x dbtで作るセキュアでアジャイルなデータ基盤
tsoshiro
1
240
Workflow automationによるインシデント原因調査の自動化
showwin
1
120
offers_20241022_imakiire.pdf
imakurusu
2
310
Featured
See All Featured
Scaling GitHub
holman
458
140k
Fantastic passwords and where to find them - at NoRuKo
philnash
50
2.8k
No one is an island. Learnings from fostering a developers community.
thoeni
19
3k
Navigating Team Friction
lara
183
14k
The Language of Interfaces
destraynor
154
24k
Six Lessons from altMBA
skipperchong
26
3.4k
Building Applications with DynamoDB
mza
90
6k
Embracing the Ebb and Flow
colly
84
4.4k
How STYLIGHT went responsive
nonsquared
95
5.1k
Fashionably flexible responsive web design (full day workshop)
malarkey
404
65k
Ruby is Unlike a Banana
tanoku
96
11k
GraphQLの誤解/rethinking-graphql
sonatard
66
9.9k
Transcript
ೖ ໊લ Rails Developer Meetup 2019 ౻ଜେհ
ࣗݾհ • ౻ଜେհ • גࣜձࣾϚνϚν • Rails ྺ 10
• https://twitter.com/ffu_ • https://github.com/fujimura
͜ͷൃදʹ͍ͭͯ • ౻ଜ໊લʹͩ͜ΘΓ͕͋Δ • ໊લʹ͍ͭͯͷৄ͍͠આ໌͕ͳ͍ͱৗʑࢥ͍ͬͯͨ • Rails͚ͩͲɺ໊લେࣄͩ͠ɺͦΖͦΖߟ͑Λ·ͱΊ͍ͨͳͱࢥͬͨ • ͪͳΈʹ WEB+DB
Press Ͱ໊લʹ͍ͭͯͷهࣄΛॻ͍͍ͯΔ
໊લͱ • ϓϩάϥϚʔ໊લΛຖ͍ͬͯΔ • ॏཁͳ͜ͱΘ͔͍ͬͯΔ • ͚Ͳɺ໊ͦͦલͬͯԿʁඞཁͳͷʁͳΜͰॏཁͳͷʁྑ໊͍લͷج४ ʁ • ࣮Α͘Θ͔Βͳ͍
͓͠ͳ͕͖ • ໊લศར • ໊લͷΈ • ͳ໊ͥલॏཁͳͷ͔ • ྑ໊͍લͱ •
࣮ ѱ໊͍લ
໊લศར • ·໊ͣલ͕͍͔ʹศརͳπʔϧͰ͋Δ͔Λ֬ೝ͠·͢
໊લΛΘͣʹϓϩάϥϜΛॻ͍ͯΈΑ͏ • ໊લͳ͠ͰϓϩάϥϜॻ͚Δ • Lazy K ͱ͍͏ϓϩάϥϛϯάݴޠͰૉΛੜ͢ΔίʔυʢͷҰ෦ʣ • ͭΒ͍ʂ (SII(S(K(S(S(K(SII(S(S(KS)(S(K(S(KS)))(S(K(S(S(KS)(SS(S(S(KS)K))(KK)))))
(S(S(KS)(S(KK)(S(KS)(S(S(KS)(S(KK)(S(KS)(S(S(KS)(S(KK)(SII))) (K(SI(KK)))))))(K(S(K(S(S(KS)(S(K(SI))(S(KK)(S(K(S(S(KS)K)(S(S(KS)K)I) ...
໊લΛͬͯϓϩάϥϜΛॻ͍ͯΈΑ͏ • Ruby ͱ͍͏ϓϩάϥϛϯάݴޠͰૉΛੜ͢Δίʔυ • ಡΊΔʂ • ཧղͰ͖Δʂ require 'prime'
Prime.each
ͦͦɺͳΜͷͨΊʹ໊લΛ͚ͭΔͷ • ୯ʹศར͔ͩΒɺΑΓਂ͍త͕͋Δ • ໊લͷҙຯΛ͔ͭͬͯɺࡉ෦Λཧղͤͣ࠶ར༻͢ΔͨΊ • ࠷ऴతʹɺੜ࢈ੑͷͨΊ
࠶ར༻ʹΑΔੜ࢈ੑͷ্ʮٕज़ʯͷࠜװ • զʑͳͥ Ruby on Rails ͕ಈ͘ͷ͔Βͳ͍͚Ͳ͑Δ • RailsɺMRIɺσʔλϕʔεɺͦͷଞϛυϧΣΞɺCPU… •
ࡉ෦Λཧղͤͣʹ࠶ར༻Ͱ͖Δ͜ͱ͕ٕज़ͷఆٛͷͻͱͭ • ͜ΕͰਓྨͷੜ࢈ੑ্͕͖ͬͯͨ • ৠؾػؔɺిؾɺίϯϐϡʔλʔ • ϓϩάϥϜͦͷҰͭ
໊લศར ·ͱΊ • ໊લ͕͋Δͱɺ࣮Λಡ·ͳͯ͘ϓϩάϥϜΛ࠶ར༻Ͱ͖Δ • ࠶ར༻Ͱ͖Δͱɺੜ࢈ੑ্͕͕Δ • ศརͲ͜ΖͰͳׂ͍ΛՌ͍ͨͯ͠Δ
໊લͷ͘͠Έ • ໊લ͕͍͔ʹϓϩάϥϛϯάͱ͍͏ٕज़ʹͱͬͯॏཁͳπʔϧͰ͋Δ͔ Θ͔Γ·ͨ͠ • ࠓ໊લͷΈΛ֬ೝ͠·͢
໊લͬͯͳʹ • Կ͔ʹ͍ͭͨϥϕϧͷ͜ͱ ΓΜ͝
໊લͷҙຯͬͯͳʹ • ϥϕϧ͕ରΛʮࢦࣔʯ͍ͯ͠Δ͜ͱ • ʮΓΜ͝ʯͬͯݴ༿ͷҙຯʁΓΜ͝ͷ͜ͱͰ͠ΐɺͱ͍͏ͷΛ͘͠ݴͬ ͨܗ ΓΜ͝
ϓϩάϥϜʹ͓͚Δ໊લͬͯͳʹ • ॲཧʹ͍ͭͨϥϕϧͷ͜ͱ • ϝιουͷྫ find_or_initialze_by model = Model.find(params[:id]) unless
model Model.new(params end
ϓϩάϥϜʹ͓͚Δ໊લͷҙຯͬͯͳʹ • ͦͷݴ༿͕ࢦࣔ͢͠ରʁ • ڍಈʁ • ͳΜ͔ײతʹҧ͍ͦ͏ • ֬ೝ͠·͢
ରΛࢦࣔ͢Δ͜ͱͩͬͨΒ • ʮ౻ଜେհʯͷҙຯɺ͜͜Ͱ͍ͬͯΔਓؒɺͱͳΔ • ʮΓΜ͝ʯͷҙຯɺੈքʹଘࡏ͢ΔΓΜ͝ͷશͯɺͱͳΔ • ※ ͜͜ΒΜͰࢦࣔઆʹແཧ͕͋Δ͜ͱ͕Θ͔ΔɻখઆͷதͷΓΜ͝ͱ ͔ •
find_or_initialize_by ͷରԿͳΜͩʁ • ͏·͍͔͘ͳͦ͏
ڍಈͩͬͨΒ • ActiveRecord::Relation#find_or_initialize_by params Λ ड͚औͬͯΫΤϦΛΈཱͯͯϨίʔυΛ୳ͯ͠…ɻ • ͜Εڍಈɻ͋ΔछϓϩάϥϜʢ࣮ʣͷҙຯͱ͍͑ͦ͏Ͱ͋Δ ͕ɺͦΕʹ͚ͨϥϕϧͱͯ͠ͷ໊લͷҙຯͰͳ͍
• ͜Εͩͱத͕Θ͔Δ͜ͱ͕ҙຯ͕Θ͔Δ͜ͱʹͳͬͯ͠·͏ • ͏·͍͔͘ͳͦ͏
ײతʹΘ͔͍ͬͯΔ • ࢦࣔͩΊɺڍಈͩΊ • ͔͠͠զʑActiveRecord::Relation#find_or_initialize_by ͱ͍͏ϝιου໊ͷҙຯ͕Θ͔Δ • ʮݟ͚ͭΔ͔ɺͳ͔ͬͨΒ࡞Δʯ • ײతʹ͜͏͍͏ʮৼΔ͍ͷઆ໌ʯ͕ҙຯͰ͋Δɺͱࢥ͍ͬͯΔͷ
Ͱ • ※ ͍͑ͯΕΦοέʔɺͱ͍͏ʮҙຯͷ༻આʯ͕ϕʔε
ϓϩάϥϜʹ͓͚Δ໊લͷҙຯ = ৼΔ͍ͷઆ໌ • find_or_initialize_by ͷҙຯʮݟ͚ͭΔ͔ɺͳ͔ͬͨΒ࡞Δʯ • ҙຯ = ৼΔ͍ͷઆ໌
• ͬ͘͠Γ͘Δʂ
໊લͷ͘͠Έ ·ͱΊ • ϓϩάϥϜʹ͓͍ͯɺ • ໊લॲཧʹ͍ͭͨϥϕϧ • ໊લͷҙຯɺৼΔ͍ͷઆ໌
ͳΜͰ໊લॏཁͳͷ • ໊લ͕ศརͳͷΘ͔Γ·ͨ͠ • ΈΘ͔͖ͬͯͨ • Ͱɺ໊લ͕ʮѱ͍ʯͱԿ͕ى͜Δͷʁઆ໌͠·͢
ѱ໊͍લͰى͖Δ͜ͱ • ཧղͷίετ͕ߴ͍ • צҧ͍Ͱόά͕ൃੜ͢Δ • => ੜ࢈ੑ͕Լ͕Δ • ςΫϊϩδʔͷత࠶ར༻ʹΑΔੜ࢈ੑͷ্ɻͦΕΛѱ໊͍લ͛
Δ • ʮΑ͍ʯ໊લॏཁ
͡Ό͋ɺԿ͕Α໊͍લͳͷ • ʰίʔυɾίϯϓϦʔτʱͰௐͯΈ·ͨ͠ • ʮ໋໊ʹ͓͍ͯ࠷ॏཁͳߟྀ͖͢ɺม͕දݱ͢ΔͷΛਖ਼֬ʹ શʹݴ͍ද͍ͯ͠Δ͜ͱʯ • McConnell, Steve. Code
Complete (Kindle Location 6490). Microsoft Press. Kindle Edition) ༁ஶऀ • ʮਖ਼֬ʯͰʮશʯͳ໊લͬͯͲ͏͍͏ঢ়ଶͰ͔͢ʁ => ॻ͍ͯͳ͍ • Կ͕ྑ໊͍લͳͷ͔ॻ͍ͯͳ͍ͷͰɺߟ͑ͯΈ·ͨ͠
Α໊͍લ = ໊લͷҙຯͱ࣮ࡍͷڍಈ͕߹͍ͬͯΔͷ • ཁɺݴ͍ͬͯΔ͜ͱͱ͍ͬͯΔ͜ͱ͕Ұக͍ͯ͠Δ໊લ • ͦΓΌͦ͏ͩΑͶɺͱ͍͏ఆٛͳΜͰ͕͢ɺࡉ͔͘ཧղ͢ΔՁ͕͋Δ • ৄ͘͠આ໌͠·͢
໊લͷҙຯͱڍಈ͕߹͍ͬͯͳ͍ͱ • ໊લͷҙຯ͕ڍಈʹରͯ͠ڱ͗͢Δ • ݴ͍ͬͯΔ͜ͱΛ͍ͬͯͳ͍ • ໊લͷҙຯ͕ڍಈʹରͯ͗͢͠Δ • ͍ͬͯΔ͜ͱΛݴ͍ͬͯͳ͍ •
ͷ͍ͣΕ͔
ύλʔϯ 1: ໊લͷҙຯ͕ڱ͗͢Δ • ʮެ։Λ࠶ઃఆʯͬͯҙຯͳͷʹɺ࣌ࠁΛ࠶ઃఆͰ͖ͯ͠·͏ • ͬͯΔ͜ͱ໊͕લͷΘΓʹ͍ class Article def
reset_publish_date(date) update!(publish_at: date) end end
ύλʔϯ 2: ໊લͷҙຯ͕͗͢Δ • check ͨ݁͠ՌͲ͏ͳΔͷʁ • login ͍ͯ͠Δͷ͕ਖ਼ʁ͍ͯ͠ͳ͍ͷ͕ਖ਼ʁ •
໊લ͔ΒΘ͔Βͳ͍ • check ͷҙຯ͕͗͢Δͷ͕ݪҼ before_action :check_login private def check_login unless current_user redirect_to login_path end; end
Α໊͍લͷ͏Ұͭͷج४ γϯϓϧ͞ • ࠷ίϯύΫτͰత֬ͳදݱΛ͏͠ • ੈͷதʹ employee ͱ͍͏ݴ༿͕͋Δ def person_who_is_hired_by_company
def empolyee
͜͜·Ͱͷ·ͱΊ • Α໊͍લ = ໊લͷҙຯͱ࣮ࡍͷڍಈ͕߹͍ͬͯΔɺγϯϓϧͳ໊લ • ͗ͨ͢Γڱ͗ͨ͢Γ͢Δͱѱ໊͍લʹͳΔ
࣮ ѱ໊͍લ • ۩ମྫΛ͓ಧ͚͠·͢
User#check_paid class User def check_paid # ... end end
User#check_paid ͱ͍͏໊લ͔Βߟ͑ΒΕΔڍಈ • ࢧ͍͕ࡁΜ͔ͩΛ Boolean Ͱฦ͢ • ࢧ͍͕ࡁΜͰ͍ͳ͍߹Ϣʔβʔʹ self.errors ΛՃ͢Δ
• ࢧ͍͕ࡁΜͰ͍ͳ͍߹ྫ֎Λు͘ • check ͷҙຯ͕͗ͯ͢ɺͲΕ͔Θ͔Βͳ͍
User#check_paid ղମ • ͦΕͧΕԼهͷ༷ʹ࣮͞Ε͍ͯΕɺΉ͜ͱͳ͍ͣ • check Λ͏ͷΛΊΔ͚ͩͰ໊લ͕ྑ͘ͳΓ·͢ class User #
ࢧ͍͕ࡁΜ͔ͩΛ Boolean Ͱฦ͢ def paid? # ... end # ࢧ͍͕ࡁΜͰ͍ͳ͍߹ϢʔβʔʹΤϥʔใΛՃ͢Δ def add_error_if_not_paid # ... end end
Notification#user (1) • ௨Λड৴͢ΔϢʔβʔͷ໊લΛ'user'ͱͨ͠ class Notification belongs_to :user end
Notification#user (2) • ʮ͕ࣗૹͬͨ௨Λফ͢ʯͨΊʹɺԼهͷΑ͏ͳίʔυΛॻ͍ͨ • Notification#user ͕ड৴ऀͳͷ͔ૹ৴ऀͳͷ͔ɺ໊લ͔ΒΘ͔Βͳ͍ sent_notifications = Notification.where(user:
current_user) # user = ड৴ͨ͠ਓͳͷͰɺࣗͷ௨͕ফ͑Δ sent_notifications.delete_all
Notification#user (3) • 'recipient'ͱ͓͚ͯ͠ɺ໊લ͔Βड৴ऀͰ͋Δ͜ͱ͕Θ͔ΔͷͰɺ͜ͷ Α͏ͳࣄނى͖ͳ͍ • ໘ͰׂʹԠ໊ͨ͡લΛ͚ͭΔΑ͏ʹ͠·͠ΐ͏ # ໊લ͔Β໌Β͔ʹม sent_notifications
= Notification.where( recipient: current_user )
༨ஊ • ͔͜͜Β໊લʹ·ͭΘΔ༨ஊͰ͢
UserRegistrationService ͷ’Service'ͬͯඞཁʁ UserRegistrationService.new.perform! UserRegistration.new.perform! # Service͍Βͳ͘ͳ͍ʁ
໊લͷωλݩ • schema.org • Google, Microsoft, Yahoo, Yandex ͕࡞ͬͨΠϯλʔωοτ༻ͷޠኮू •
ςʔϒϧઃܭΜͩ࣌͜͜ΛݟΕ݁ߏࡌ͍ͬͯΔ
ࣙॻ.app • ໊લͷͨΊʹ࠷ॏཁͳπʔϧࣙॻ • Mac OS X ͷ ࣙॻ.app ਆ
• ͳΜͱӳӳࣙయɺྨޠࣙయɺӳࣙయɺӳࣙయ͕ɺશ෦ແྉͰೖ͍ͬͯΔ • ʮڥઃఆʯ͔ΒՃͰ͖ΔͷͰɺ·ͩͷํ͍·͙͢Ճ͠Α͏