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
Coercion in Ruby
Search
Grzegorz Witek
May 09, 2018
Technology
1
150
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
78
Writing config files in Ruby
arnvald
0
130
Speaking at RDRC
arnvald
0
140
Read more
arnvald
2
95
Your API is too slow!
arnvald
0
710
The simplest gem you'll ever use
arnvald
0
100
International to global
arnvald
0
100
Patterns, patterns everywhere
arnvald
0
130
Nomadic programmer - Baruco 2014 edition
arnvald
0
130
Other Decks in Technology
See All in Technology
[CV勉強会@関東 World Model 読み会] Orbis: Overcoming Challenges of Long-Horizon Prediction in Driving World Models (Mousakhan+, NeurIPS 2025)
abemii
0
130
OCI Database Management サービス詳細
oracle4engineer
PRO
1
7.4k
予期せぬコストの急増を障害のように扱う――「コスト版ポストモーテム」の導入とその後の改善
muziyoshiz
1
1.8k
AIエージェントを開発しよう!-AgentCore活用の勘所-
yukiogawa
0
160
顧客との商談議事録をみんなで読んで顧客解像度を上げよう
shibayu36
0
230
レガシー共有バッチ基盤への挑戦 - SREドリブンなリアーキテクチャリングの取り組み
tatsukoni
0
210
Bedrock PolicyでAmazon Bedrock Guardrails利用を強制してみた
yuu551
0
220
Kiro IDEのドキュメントを全部読んだので地味だけどちょっと嬉しい機能を紹介する
khmoryz
0
180
配列に見る bash と zsh の違い
kazzpapa3
1
140
今日から始めるAmazon Bedrock AgentCore
har1101
4
410
15 years with Rails and DDD (AI Edition)
andrzejkrzywda
0
190
超初心者からでも大丈夫!オープンソース半導体の楽しみ方〜今こそ!オレオレチップをつくろう〜
keropiyo
0
110
Featured
See All Featured
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
254
22k
Breaking role norms: Why Content Design is so much more than writing copy - Taylor Woolridge
uxyall
0
160
Everyday Curiosity
cassininazir
0
130
Building Applications with DynamoDB
mza
96
6.9k
Art, The Web, and Tiny UX
lynnandtonic
304
21k
AI in Enterprises - Java and Open Source to the Rescue
ivargrimstad
0
1.1k
Large-scale JavaScript Application Architecture
addyosmani
515
110k
30 Presentation Tips
portentint
PRO
1
210
Done Done
chrislema
186
16k
End of SEO as We Know It (SMX Advanced Version)
ipullrank
3
3.9k
Documentation Writing (for coders)
carmenintech
77
5.3k
Into the Great Unknown - MozCon
thekraken
40
2.3k
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