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
4
990
AIの時代で我々はどのようにコードを書くのか
fujimura
3
780
SaaSを作るという仕事について
fujimura
12
5.7k
一文字エイリアスのすすめ
fujimura
0
320
現役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
みんなでプロポーザルを書いてみた
yuriko1211
0
280
Realtime API 入門
riofujimon
0
150
Duckdb-Wasmでローカルダッシュボードを作ってみた
nkforwork
0
130
Outline View in SwiftUI
1024jp
1
340
2024/11/8 関西Kaggler会 2024 #3 / Kaggle Kernel で Gemma 2 × vLLM を動かす。
kohecchi
5
960
最新TCAキャッチアップ
0si43
0
200
What’s New in Compose Multiplatform - A Live Tour (droidcon London 2024)
zsmb
1
480
Amazon Bedrock Agentsを用いてアプリ開発してみた!
har1101
0
340
TypeScriptでライブラリとの依存を限定的にする方法
tutinoko
3
720
.NET のための通信フレームワーク MagicOnion 入門 / Introduction to MagicOnion
mayuki
1
1.8k
OSSで起業してもうすぐ10年 / Open Source Conference 2024 Shimane
furukawayasuto
0
110
watsonx.ai Dojo #4 生成AIを使ったアプリ開発、応用編
oniak3ibm
PRO
1
190
Featured
See All Featured
Imperfection Machines: The Place of Print at Facebook
scottboms
265
13k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
44
6.8k
Java REST API Framework Comparison - PWX 2021
mraible
PRO
28
8.2k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
28
9.1k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
356
29k
Why Our Code Smells
bkeepers
PRO
334
57k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
131
33k
Automating Front-end Workflow
addyosmani
1366
200k
Testing 201, or: Great Expectations
jmmastey
38
7.1k
Optimizing for Happiness
mojombo
376
70k
The World Runs on Bad Software
bkeepers
PRO
65
11k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
44
2.2k
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 ਆ
• ͳΜͱӳӳࣙయɺྨޠࣙయɺӳࣙయɺӳࣙయ͕ɺશ෦ແྉͰೖ͍ͬͯΔ • ʮڥઃఆʯ͔ΒՃͰ͖ΔͷͰɺ·ͩͷํ͍·͙͢Ճ͠Α͏