Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Speaker Deck
PRO
Sign in
Sign up for free
Coercion in Ruby
Grzegorz Witek
May 09, 2018
Technology
1
33
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
38
Writing config files in Ruby
arnvald
0
25
Speaking at RDRC
arnvald
0
28
Read more
arnvald
2
28
Your API is too slow!
arnvald
0
360
The simplest gem you'll ever use
arnvald
0
18
International to global
arnvald
0
20
Patterns, patterns everywhere
arnvald
0
17
Nomadic programmer - Baruco 2014 edition
arnvald
0
74
Other Decks in Technology
See All in Technology
⚡Lightdashを試してみた
k_data_analyst
0
220
LINEスタンプの実例紹介 小さく始める障害検知・対応・振り返りの 改善プラクティス
line_developers
PRO
3
2k
失敗を経験したあなたへ〜建設的なインシデントの振り返りを行うために実践するべきこと〜
nobuakikikuchi
0
210
[SRE NEXT 2022]ヤプリのSREにおけるセキュリティ強化の取り組みを公開する
mmochi23
1
870
ドキュメントの翻訳に必要なこと
mayukosawai
0
190
AWS ChatbotでEC2インスタンスを 起動できるようにした
iwamot
0
170
大きくなるチームを支える技術 / Technology to support a growing SCX team
ku00
0
130
[AKIBA.AWS] それ、t2.micro選んで大丈夫?
tsukuboshi
0
380
長年運用されてきたモノリシックアプリケーションをコンテナ化しようとするとどんな問題に遭遇するか? / SRE NEXT 2022
nulabinc
PRO
15
8.1k
一人から始めるプロダクトSRE / How to start SRE in a product team, all by yourself
vtryo
4
3k
街じゅうを"駅前化"する電動マイクロモビリティのシェアサービス「LUUP」のIoTとSRE
0gm
1
1k
TypeScript 4.7と型レベルプログラミング
uhyo
6
3.6k
Featured
See All Featured
Intergalactic Javascript Robots from Outer Space
tanoku
261
25k
How to Ace a Technical Interview
jacobian
265
21k
Adopting Sorbet at Scale
ufuk
63
7.5k
Raft: Consensus for Rubyists
vanstee
126
5.4k
Designing the Hi-DPI Web
ddemaree
272
32k
Building Flexible Design Systems
yeseniaperezcruz
310
33k
The MySQL Ecosystem @ GitHub 2015
samlambert
238
11k
4 Signs Your Business is Dying
shpigford
169
20k
What’s in a name? Adding method to the madness
productmarketing
11
1.5k
Ruby is Unlike a Banana
tanoku
91
9.2k
Git: the NoSQL Database
bkeepers
PRO
415
59k
The Straight Up "How To Draw Better" Workshop
denniskardys
225
120k
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