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
110
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
65
Writing config files in Ruby
arnvald
0
100
Speaking at RDRC
arnvald
0
99
Read more
arnvald
2
74
Your API is too slow!
arnvald
0
680
The simplest gem you'll ever use
arnvald
0
69
International to global
arnvald
0
66
Patterns, patterns everywhere
arnvald
0
80
Nomadic programmer - Baruco 2014 edition
arnvald
0
120
Other Decks in Technology
See All in Technology
Reach American Airlines®️ Instantly: 19 Calling Methods for Fast Support in the USA
flyamerican
1
180
Lakebaseを使ったAIエージェントを実装してみる
kameitomohiro
0
180
公開初日に Gemini CLI を試した話や FFmpeg と組み合わせてみた話など / Gemini CLI 初学者勉強会(#AI道場)
you
PRO
0
1k
AWS CDK 入門ガイド これだけは知っておきたいヒント集
anank
5
600
インフラ寄りSREの生存戦略
sansantech
PRO
9
3.4k
「Chatwork」のEKS環境を支えるhelmfileを使用したマニフェスト管理術
hanayo04
1
240
事例で学ぶ!B2B SaaSにおけるSREの実践例/SRE for B2B SaaS: A Real-World Case Study
bitkey
1
340
AIエージェントが書くのなら直接CloudFormationを書かせればいいじゃないですか何故AWS CDKを使う必要があるのさ
watany
18
7k
VS CodeとGitHub Copilotで爆速開発!アップデートの波に乗るおさらい会 / Rapid Development with VS Code and GitHub Copilot: Catch the Latest Wave
yamachu
2
340
SREのためのeBPF活用ステップアップガイド
egmc
2
900
AI エージェントと考え直すデータ基盤
na0
18
7.3k
20250707-AI活用の個人差を埋めるチームづくり
shnjtk
6
4.1k
Featured
See All Featured
The Language of Interfaces
destraynor
158
25k
Documentation Writing (for coders)
carmenintech
72
4.9k
Fashionably flexible responsive web design (full day workshop)
malarkey
407
66k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
331
22k
Six Lessons from altMBA
skipperchong
28
3.9k
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.4k
Learning to Love Humans: Emotional Interface Design
aarron
273
40k
How to Think Like a Performance Engineer
csswizardry
25
1.7k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
30
2.1k
Making Projects Easy
brettharned
116
6.3k
Faster Mobile Websites
deanohume
307
31k
Docker and Python
trallard
45
3.5k
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