$30 off During Our Annual Pro Sale. View Details »
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Coercion in Ruby
Search
Grzegorz Witek
May 09, 2018
Technology
1
140
Coercion in Ruby
Grzegorz Witek
May 09, 2018
Tweet
Share
More Decks by Grzegorz Witek
See All by Grzegorz Witek
One Year with Hanami
arnvald
0
73
Writing config files in Ruby
arnvald
0
120
Speaking at RDRC
arnvald
0
130
Read more
arnvald
2
90
Your API is too slow!
arnvald
0
710
The simplest gem you'll ever use
arnvald
0
93
International to global
arnvald
0
85
Patterns, patterns everywhere
arnvald
0
110
Nomadic programmer - Baruco 2014 edition
arnvald
0
130
Other Decks in Technology
See All in Technology
AI駆動開発の実践とその未来
eltociear
1
390
AgentCoreとStrandsで社内d払いナレッジボットを作った話
motojimayu
0
180
ハッカソンから社内プロダクトへ AIエージェント「ko☆shi」開発で学んだ4つの重要要素
sonoda_mj
5
840
アプリにAIを正しく組み込むための アーキテクチャ── 国産LLMの現実と実践
kohju
0
120
打 造 A I 驅 動 的 G i t H u b ⾃ 動 化 ⼯ 作 流 程
appleboy
0
370
Bedrock AgentCore Memoryの新機能 (Episode) を試してみた / try Bedrock AgentCore Memory Episodic functionarity
hoshi7_n
1
920
re:Invent2025 3つの Frontier Agents を紹介 / introducing-3-frontier-agents
tomoki10
0
290
SQLだけでマイグレーションしたい!
makki_d
0
1.1k
S3を正しく理解するための内部構造の読解
nrinetcom
PRO
3
200
Kiro を用いたペアプロのススメ
taikis
2
880
AIプラットフォームにおけるMLflowの利用について
lycorptech_jp
PRO
1
170
IAMユーザーゼロの運用は果たして可能なのか
yama3133
2
500
Featured
See All Featured
世界の人気アプリ100個を分析して見えたペイウォール設計の心得
akihiro_kokubo
PRO
63
35k
GitHub's CSS Performance
jonrohan
1032
470k
The innovator’s Mindset - Leading Through an Era of Exponential Change - McGill University 2025
jdejongh
PRO
1
63
Data-driven link building: lessons from a $708K investment (BrightonSEO talk)
szymonslowik
0
840
The SEO Collaboration Effect
kristinabergwall1
0
300
First, design no harm
axbom
PRO
1
1k
KATA
mclloyd
PRO
33
15k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
49
3.2k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
21
1.3k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
37
2.7k
VelocityConf: Rendering Performance Case Studies
addyosmani
333
24k
[SF Ruby Conf 2025] Rails X
palkan
0
540
Transcript
Coercion in Ruby Between the strong and weak typing
Grzegorz Witek
Strong vs. weak typing $> 3 + “a” Python: unsupported
operand type(s) for +: 'int' and 'str' Ruby: String can't be coerced into Fixnum Javascript: “3a”
Strong vs. weak typing $> 3 + “a” Python: unsupported
operand type(s) for +: 'int' and 'str' Ruby: String can't be coerced into Fixnum Javascript: “3a”
Coercion in Ruby class Money < Struct.new(:amount) def *(value) amount
* value end end
How do I Ruby? money = Money.new(3) money * 2
# => 6 2 * money # => ERROR U FAIL
Bad solution class Fixnum alias :old_multiply :* def *(val) if
defined?(Money) && val.is_a?(Money) return self * val.amount else old_multiply(val) end end end
Bad solution Pros: works Cons: it’s wrong on as many
levels as you can imagine
Good solution class Money < Struct.new(:amount) def *(value) amount
* value end def coerce(other) [self, other] end end
Good solution def coerce(other) [self, other] end
Good solution def coerce(other) [other, amount] end
How does it work? Short answer: when Ruby can’t handle
the param type, it calls arg.coerce(self) it gets 2 elements array, and calls array[0].method(array[1])
How does it work? Fixnum#*(Money) => omg, what to do?
How does it work? Fixnum#(Money)* => omg, what to do?
Money#coerce => [Money, Fixnum]
How does it work? Fixnum#*(Money) => omg, what to do?
Money#coerce => [Money, Fixnum] Money#*(Fixnum) => I know how to handle it!
Coercion in Ruby Thanks! Grzegorz Witek @arnvald