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
⚡️Ruby、オブジェクト指向、デザイン / Ruby, OOP, Design
Search
Takumi Shotoku
November 05, 2020
Technology
1.8k
5
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
⚡️Ruby、オブジェクト指向、デザイン / Ruby, OOP, Design
Omotesando.rb #56
https://omotesandorb.connpass.com/event/192551/
Takumi Shotoku
November 05, 2020
More Decks by Takumi Shotoku
See All by Takumi Shotoku
TypeProf 開発レポート 2026-05 / TypeProf Dev Report 2026-05
sinsoku
1
140
Automatically generating types by running tests
sinsoku
4
19k
滅・サービスクラス🔥 / Destruction Service Class
sinsoku
8
2.9k
テストを書かないためのテスト/ Tests for not writing tests
sinsoku
1
310
ドメインの本質を掴む / Get the essence of the domain
sinsoku
2
350
"型"のあるRailsアプリケーション開発 / Typed Rails application development
sinsoku
11
3k
Let's get started with Ruby && Rails Tips
sinsoku
0
500
LTの敷居を下げる / Lower the threshold for LT
sinsoku
2
440
CircleCIの高速化🚀 / CircleCI faster
sinsoku
3
1.5k
Other Decks in Technology
See All in Technology
Docker Desktop不要の時代が来る? WSL標準の「wslc」で Linuxコンテナを動かしてみた.
ueponx
0
770
DMM.com 購入改善推進チーム におけるCodeRabbitを用いた レビューフロー改善の一例
ysknsid25
1
490
Terraform共通モジュールをチーム横断で“変えられる”運用へ ― リリースと適用の分離
kekke_n
0
1.3k
SRE依存からの脱却 運用を開 発チームへ移す、 フルサイ クル開 発体制の実践
joooee0000
0
1.3k
Keeping applications secure by evolving OAuth 2.0 and OpenID Connect
ahus1
PRO
1
150
初めてのDatabricks勉強会
taka_aki
2
240
デジタル・デザイン:次の50年を描く「進化する青写真」
y150saya
0
820
SRE歴2ヶ月でも開発6年の知見を活かして、チームで止まっていた環境改善を前に進めた話
a_ono
1
210
AIに「使われる」時代のSaaS戦略 〜既存WebAPIのMCPサーバー化における開発ノウハウ〜
ekispert_api
0
300
4人目のSREはAgent
tanimuyk
0
400
どうして今サーバーサイドKotlinを選択したのか
nealle
0
210
Empower GenAI with Agile - あなたのアジャイルが生成AIのバフになる仕組み
hageyahhoo
0
130
Featured
See All Featured
Un-Boring Meetings
codingconduct
0
330
Noah Learner - AI + Me: how we built a GSC Bulk Export data pipeline
techseoconnect
PRO
0
210
A brief & incomplete history of UX Design for the World Wide Web: 1989–2019
jct
2
410
Technical Leadership for Architectural Decision Making
baasie
3
430
What the history of the web can teach us about the future of AI
inesmontani
PRO
1
630
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
141
35k
Bash Introduction
62gerente
615
220k
Side Projects
sachag
455
43k
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
11
960
16th Malabo Montpellier Forum Presentation
akademiya2063
PRO
0
170
Designing for Performance
lara
611
70k
How to build an LLM SEO readiness audit: a practical framework
nmsamuel
1
800
Transcript
⚡ Ruby、オブジェクト指向、設計 Omotesando.rb #56 2020/11/05 1
自己紹介 • 名前: 神速 • 会社: メドピア株式会社 • 所属: CTO室SRE
• GitHub: @sinsoku (画像右上) • Twitter: @sinsoku_listy (画像右下) 2
⚠ ちゃんと学びたい人は ! をたくさん読んでください。 3
オブジェクト指向について • プログラミングの設計手法 • データと処理をまとめる • Rubyのすべてはオブジェクト • 純粋なオブジェクト指向言語 4
オブジェクト指向は設計手法 プログラミング言語には依存しない 5
C言語でも(頑張れば)OOPできる1 #include <stdio.h> struct user_data { char name[20]; }; char
*name(struct user_data *this) { return this -> name; } char first(struct user_data *this) { return this -> name[0]; } struct user_class { struct user_data obj; char *(*name)(struct user_data *this); char (*first)(struct user_data *this); }; int main() { struct user_data this = { "foo" }; struct user_class u = { this, name, first }; printf("%s\n", u.name(&this)); printf("%c\n", u.first(&this)); } 1 普通はやらないと思う 6
Rubyだと簡単 class User attr_reader :name def initialize(name) @name = name
end def first name[0] end end u = User.new("foo") puts u.name puts u.first 7
オブジェクト指向な設計とは? 8
9
オブジェクト指向 • 設計に正解は存在しない • UserLogin, User#login のどちらでも良い • 他メンバーが読みやすいかどうか •
名前から処理を予想できるか? • ビジネスの用語とズレがないか? 10
オブジェクト指向のSOLID • 単一責任の原則 • 解放閉鎖の原則 • リスコフの置換原則 • インターフェース分離の原則 •
依存性逆転の原則 11
12
普段そこまで考えてない 13
コードは短く、読みやすく書く2 • 短いコードは正義 • クラスやモジュールを作り過ぎない • 最初は単純なメソッドで済ませる • メタプロで短くするのは控える •
パフォーマンスは後で考える " 2 去年の銀座Railsのスライドから引用 14
考えていること 15
get_xxx を避ける Rubyだとgetterは名詞を使うことが多い。 class User # bad # def get_items;
end # good def my_items; end end 16
! 英語にして違和感がない3 クラスを名詞、メソッドを動詞にして違和感ないか? class User # user login def login;
end # user search by word def search_by(word); end end メソッドが長い場合、クラスを抽出できる可能性がある。 3 Railsのメソッド名を参考にする事が多い。 17
インスタンス変数を使うようにする ! な例 class Shop def total_price(item, coupon: nil) if
coupon item.price - coupon.discount else item.price end end end 18
インスタンス変数を使うようにする ! な例。インスタンス変数の分だけ短くなる。 class Item def total_price(coupon: nil) if coupon
price - coupon.discount else price end end end 19
脳内メモリを使わない foo -> bar -> buz -> piyo の ような呼び出しだと変数の値
を覚えるのが大変。 class A def foo bar end def bar buz end def buz piyo end end 20
脳内メモリを使わない 一度に覚えておくことを減ら したい。 class A def foo x = bar
y = buz z = piyo end end 21
イミュータブル ! 引数を破壊したり、ループを使っている。 def cool_items(items, service_charge) items.each do |item| next
if item.xxx? item.price += service_charge end items end 22
イミュータブル ! 関数型プログラミング的 def cool_items(items, service_charge) items.select { |item| item.xxx?
} .map { |item| Item.new(price: item.price + service_charge) end end 23
まとめ? • 設計の用語はコミュニケーション用 • みんな普段そこまで考えているの・・・? • 短いコードを書こうとすると良い感じになる • 気がする 24