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
庭と負債
fujimura
2
270
AIの時代で我々はどのようにコードを書くのか
fujimura
3
770
SaaSを作るという仕事について
fujimura
12
5.7k
一文字エイリアスのすすめ
fujimura
0
310
現役CTOが語る!RubyKaigiの楽しみ方
fujimura
0
1.2k
いかにして文系新卒エンジニアが「大きな問い」を大事にするCTOになったのか
fujimura
2
660
Kaigi on Rails 2022 - 既存Railsアプリ攻略法 CTOが見ること・やること・考えること
fujimura
14
3.9k
SimpleDelegator活用のご提案
fujimura
0
1.5k
それPostgreSQLでできるよ @ Rails Developer Meetup 2018 Day 1
fujimura
10
3.9k
Other Decks in Programming
See All in Programming
Quine, Polyglot, 良いコード
qnighy
4
640
シェーダーで魅せるMapLibreの動的ラスタータイル
satoshi7190
1
480
2024/11/8 関西Kaggler会 2024 #3 / Kaggle Kernel で Gemma 2 × vLLM を動かす。
kohecchi
5
910
Streams APIとTCPフロー制御 / Web Streams API and TCP flow control
tasshi
2
350
Pinia Colada が実現するスマートな非同期処理
naokihaba
4
220
Duckdb-Wasmでローカルダッシュボードを作ってみた
nkforwork
0
120
現場で役立つモデリング 超入門
masuda220
PRO
15
3.2k
ECS Service Connectのこれまでのアップデートと今後のRoadmapを見てみる
tkikuc
2
250
ペアーズにおけるAmazon Bedrockを⽤いた障害対応⽀援 ⽣成AIツールの導⼊事例 @ 20241115配信AWSウェビナー登壇
fukubaka0825
6
1.8k
Click-free releases & the making of a CLI app
oheyadam
2
110
TypeScript Graph でコードレビューの心理的障壁を乗り越える
ysk8hori
2
1.1k
Laravel や Symfony で手っ取り早く OpenAPI のドキュメントを作成する
azuki
1
110
Featured
See All Featured
The Invisible Side of Design
smashingmag
298
50k
How GitHub (no longer) Works
holman
310
140k
Side Projects
sachag
452
42k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
42
9.2k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
364
24k
4 Signs Your Business is Dying
shpigford
180
21k
5 minutes of I Can Smell Your CMS
philhawksworth
202
19k
Visualization
eitanlees
145
15k
Git: the NoSQL Database
bkeepers
PRO
427
64k
What's new in Ruby 2.0
geeforr
343
31k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
232
17k
Keith and Marios Guide to Fast Websites
keithpitt
409
22k
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 ਆ
• ͳΜͱӳӳࣙయɺྨޠࣙయɺӳࣙయɺӳࣙయ͕ɺશ෦ແྉͰೖ͍ͬͯΔ • ʮڥઃఆʯ͔ΒՃͰ͖ΔͷͰɺ·ͩͷํ͍·͙͢Ճ͠Α͏