Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
お前はまだRubyの 型の強さを知らない
Search
すぎうり
June 23, 2026
Programming
37
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
お前はまだRubyの 型の強さを知らない
すぎうり
June 23, 2026
More Decks by すぎうり
See All by すぎうり
BareRuby ~組み込み向けRubyAOTコンパイラ~
uproad3
0
29
AIコードアポカリプス
uproad3
0
58
負債解消という仕事は儲かる
uproad3
8
4.8k
RaspberryPi Picoの表現力の拡張 ~アナログコンピュータとの出会い~
uproad3
0
47
Rubyのメソッド解決チェーン
uproad3
0
29
動的型解析器 Ethotrace
uproad3
0
21
お前はまだRubyの 型システムを知らない
uproad3
1
98
UdonRubyの実現可能性について
uproad3
0
44
RubyKaja 2026
uproad3
0
37
Other Decks in Programming
See All in Programming
Embedded Swiftで作る自作USBデバイス によるiOSデバイスの自動テスト / iOSDC Japan 2026 glassfiber
glassfiber
0
330
外から家のPCの電源入れたい!をPHPで解決する
kawashimo0
1
130
AI が書く Go コードの品質を劇的に向上させる Linter: “declscope”
mpyw
0
490
Java 27新機能 / Java 27 new features
kishida
2
220
wkhtmltopdfの次どうするか問題2026
willnet
2
1.9k
Streamlitで実現する自然言語データアプリ開発
ayumu_yamaguchi
1
360
When benchmarks go bad - what I learned from measuring performance wrong
hollycummins
0
200
re:Inventに行く前に知っておきたい現地参加のノウハウ
nokomoro3
0
400
Unity6 URPによる海底をテーマにしたVJシステムの解説 | Unity Shader 完全に理解した 勉強会
gam0022
0
350
半永久的に提供し続けられるプライベートクラウドを目指して ― 利用者の認知負荷を抑えるAPI抽象化とハードウェア世代交代の基盤設計
tomokon
0
480
The Rails Doctrine Decade
koic
2
560
IBM Bob でAI駆動開発『QC-Playbook』
ichirokano2
0
110
Featured
See All Featured
The Curious Case for Waylosing
cassininazir
1
560
Building the Perfect Custom Keyboard
takai
2
900
Claude Code どこまでも/ Claude Code Everywhere
nwiizo
68
58k
Tell your own story through comics
letsgokoyo
1
1.1k
The browser strikes back
jonoalderson
0
1.8k
Embracing the Ebb and Flow
colly
88
5.2k
Designing Powerful Visuals for Engaging Learning
tmiket
1
600
Building a Modern Day E-commerce SEO Strategy
aleyda
45
9.2k
Six Lessons from altMBA
skipperchong
29
4.5k
Joys of Absence: A Defence of Solitary Play
codingconduct
1
540
Digital Projects Gone Horribly Wrong (And the UX Pros Who Still Save the Day) - Dean Schuster
uxyall
1
3.3k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
12
1.3k
Transcript
お前はまだ Rubyの 型の強さを知らない すぎうり Rubyの型シリーズ#2
自己紹介 すぎうり • Twitter:@uproad3 • Ruby歴20年 • VRChat歴7年 • 仕事:Rails |
AWS | LT芸人 • 趣味:アーキテクト | リファクタリング | 電子工作 • 言語:Ruby | C# | C | JS | ほかいろいろ • 技術:Terraform | Unity | Ubuntu | MySQL | RaspberryPi • 悲しきフルスタックエンジニア • 最近はClaudeをシバきまわしている • 特に型に強い思い入れがあるわけではない
前回のおさらい 「Rubyには型がない」 ↓ 「Rubyには型注釈がないだけ」 型検査・型強度・型互換・型変性・ポリモーフィズム... Rubyには型システムがちゃんとある
補足 このシリーズにおける型とは”インスタンス ”の型 変数はすべて型なし(あえて言うならインスタンス型) Rubyの世界では値はすべてインスタンス
型の強さとは 強い型 暗黙的型変換をする 勝手に型を合わせてくれる 便利だけど予測が難しい 弱い型 暗黙的型変換をしない 型が合わなければエラー 意図が明確になる
暗黙的型変換をする言語 JavaScript 1 + "2" // => "12" ← 数値が文字列に化ける
C言語 int i = 1; float f = 2.5; float result = i + f; // int が暗黙的に float に昇格 → 便利だが、意図しない変換が潜む
Rubyは変換しない 3 + '1' # => TypeError: String can't be
coerced into Integer Rubyは黙って変換しない。おかしければ怒る。 これが「型の強さ」 変換したいなら明示的に: 3 + '1'.to_i # => 4 3.to_s + '1' # => '31'
前提:’+’は実はメソッド呼び出し Rubyの演算子は「メソッド」として定義されている # 1 + 2 は実はこういうこと 1.+(2) # =>
3 # 自分で定義もできる class MyVal def +(other) MyVal.new(@val + other.val) end end
String#+ は相手の to_str を呼ぶ コード例 # to_str なし → TypeError
class MyStr def initialize(s); @s = s; end end "Hello " + MyStr.new("world") # => TypeError # to_str あり → 成功 class MyStr def to_str; @s; end end "Hello " + MyStr.new("world") # => "Hello world" String#+ の擬似コード def +(other) if other.respond_to?(:to_str) # to_str を呼んで文字列化 concat(other.to_str) else raise TypeError, "no implicit conversion" end end ※ 実際はC実装。Ruby擬似コードに変換
Numeric#+ は相手の coerce を呼ぶ コード例 class Money def initialize(v); @v
= v; end def coerce(other) [Money.new(other), self] end def +(other) Money.new(@v + other.to_i) end end 2 + Money.new(100) # => Money(102) Numeric#+ の擬似コード def +(other) # 同じ型なら直接計算 return numeric_add(other) if other.is_a?(Numeric) # 異なる型は coerce を期待 x, y = other.coerce(self) x + y rescue TypeError raise TypeError, "can't coerce" end ※ 実際はC実装。Ruby擬似コードに変換
Rubyは相手が変換方法を知っていることを期待する String#+ to_str を持ってる? 持っていれば文字列 として扱う 持っていなければ TypeError Numeric#+ coerce
を持ってる? 持っていれば演算可 能として扱う 持っていなければ TypeError 共通の思想 暗黙的に変換しない 変換方法を知って いるかを確かめて から使う 型は強い。でも、変換方法を提供できる ——これがRubyの「開かれた強さ」
まとめ Rubyの型は「強い」 型が合わなければ TypeError で落とす 変換メソッドの呼び出し(to_str, coerce) + ダックタイピング → 暗黙的型変換をしない
→ 意図を明示することがRubyの流儀 → 既存の型に型演算を追加できる
次回 お前はまだ Rubyの型の互換性を知らない