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
24
14k
入門 名前
Fujimura Daisuke
March 23, 2019
Tweet
Share
More Decks by Fujimura Daisuke
See All by Fujimura Daisuke
庭と負債
fujimura
4
1.1k
AIの時代で我々はどのようにコードを書くのか
fujimura
3
850
SaaSを作るという仕事について
fujimura
12
5.7k
一文字エイリアスのすすめ
fujimura
0
330
現役CTOが語る!RubyKaigiの楽しみ方
fujimura
0
1.2k
いかにして文系新卒エンジニアが「大きな問い」を大事にするCTOになったのか
fujimura
2
670
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
ブラウザ単体でmp4書き出すまで - muddy-web - 2024-12
yue4u
2
460
テストケースの名前はどうつけるべきか?
orgachem
PRO
0
130
return文におけるstd::moveについて
onihusube
1
750
range over funcの使い道と非同期N+1リゾルバーの夢 / about a range over func
mackee
0
110
tidymodelsによるtidyな生存時間解析 / Japan.R2024
dropout009
1
740
採用事例の少ないSvelteを選んだ理由と それを正解にするためにやっていること
oekazuma
2
1k
第5回日本眼科AI学会総会_AIコンテスト_3位解法
neilsaw
0
170
Zoneless Testing
rainerhahnekamp
0
120
htmxって知っていますか?次世代のHTML
hiro_ghap1
0
330
Cloudflare MCP ServerでClaude Desktop からWeb APIを構築
kutakutat
1
530
たのしいparse.y
ydah
3
120
useSyncExternalStoreを使いまくる
ssssota
6
1k
Featured
See All Featured
Practical Orchestrator
shlominoach
186
10k
A better future with KSS
kneath
238
17k
Thoughts on Productivity
jonyablonski
67
4.4k
Automating Front-end Workflow
addyosmani
1366
200k
Stop Working from a Prison Cell
hatefulcrawdad
267
20k
Java REST API Framework Comparison - PWX 2021
mraible
PRO
28
8.3k
Building Applications with DynamoDB
mza
91
6.1k
Optimizing for Happiness
mojombo
376
70k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
28
2.1k
Raft: Consensus for Rubyists
vanstee
137
6.7k
The MySQL Ecosystem @ GitHub 2015
samlambert
250
12k
Testing 201, or: Great Expectations
jmmastey
40
7.1k
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 ਆ
• ͳΜͱӳӳࣙయɺྨޠࣙయɺӳࣙయɺӳࣙయ͕ɺશ෦ແྉͰೖ͍ͬͯΔ • ʮڥઃఆʯ͔ΒՃͰ͖ΔͷͰɺ·ͩͷํ͍·͙͢Ճ͠Α͏