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
120
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
110
Speaking at RDRC
arnvald
0
110
Read more
arnvald
2
77
Your API is too slow!
arnvald
0
690
The simplest gem you'll ever use
arnvald
0
75
International to global
arnvald
0
72
Patterns, patterns everywhere
arnvald
0
87
Nomadic programmer - Baruco 2014 edition
arnvald
0
120
Other Decks in Technology
See All in Technology
Kubernetes における cgroup driver のしくみ: runwasi の bugfix より
z63d
2
110
制約理論(ToC)入門
recruitengineers
PRO
9
3.7k
Nstockの一人目エンジニアが 3年間かけて向き合ってきた セキュリティのこととこれから〜あれから半年〜
yo41sawada
0
170
DDD集約とサービスコンテキスト境界との関係性
pandayumi
2
210
AIエージェントの活用に重要な「MCP (Model Context Protocol)」とは何か
masayamoriofficial
0
250
「魔法少女まどか☆マギカ Magia Exedra」のグローバル展開を支える、開発チームと翻訳チームの「意識しない協創」を実現するローカライズシステム
gree_tech
PRO
0
430
Kiroと学ぶコンテキストエンジニアリング
oikon48
5
5.5k
Automating Web Accessibility Testing with AI Agents
maminami373
0
290
Webブラウザ向け動画配信プレイヤーの 大規模リプレイスから得た知見と学び
yud0uhu
0
120
つくって納得、つかって実感! 大規模言語モデルことはじめ
recruitengineers
PRO
32
12k
実践アプリケーション設計 ②トランザクションスクリプトへの対応
recruitengineers
PRO
4
1.2k
MCPで変わる Amebaデザインシステム「Spindle」の開発
spindle
PRO
2
2k
Featured
See All Featured
Embracing the Ebb and Flow
colly
87
4.8k
It's Worth the Effort
3n
187
28k
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
Reflections from 52 weeks, 52 projects
jeffersonlam
351
21k
jQuery: Nuts, Bolts and Bling
dougneiner
64
7.9k
Agile that works and the tools we love
rasmusluckow
330
21k
A Modern Web Designer's Workflow
chriscoyier
696
190k
Building Flexible Design Systems
yeseniaperezcruz
328
39k
Six Lessons from altMBA
skipperchong
28
4k
Into the Great Unknown - MozCon
thekraken
40
2k
Build your cross-platform service in a week with App Engine
jlugia
231
18k
Become a Pro
speakerdeck
PRO
29
5.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