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
100
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
60
Writing config files in Ruby
arnvald
0
92
Speaking at RDRC
arnvald
0
84
Read more
arnvald
2
68
Your API is too slow!
arnvald
0
660
The simplest gem you'll ever use
arnvald
0
62
International to global
arnvald
0
58
Patterns, patterns everywhere
arnvald
0
67
Nomadic programmer - Baruco 2014 edition
arnvald
0
120
Other Decks in Technology
See All in Technology
地味にいろいろあった! 2025春のAmazon Bedrockアップデートおさらい
minorun365
PRO
2
570
Асинхронная коммуникация в Go: от понятного к душному. Дима Некрасов, Otello, 2ГИС
lamodatech
0
2k
OPENLOGI Company Profile for engineer
hr01
1
26k
MCP でモノが動くとおもしろい/It is interesting when things move with MCP
bitkey
1
120
Azure × MCP 入門
ry0y4n
8
1.5k
ビジネスとデザインとエンジニアリングを繋ぐために 一人のエンジニアは何ができるか / What can a single engineer do to connect business, design, and engineering?
kaminashi
2
890
Compose におけるパスワード自動入力とパスワード保存
tonionagauzzi
0
200
本当に必要なのは「QAという技術」だった!試行錯誤から生まれた、品質とデリバリーの両取りアプローチ / Turns Out, "QA as a Discipline" Was the Key!
ar_tama
9
3.5k
雑に疎通確認だけしたい...せや!CloudShell使ったろ!
alchemy1115
0
190
Twelve-Factor-Appから学ぶECS設計プラクティス/ECS practice for Twelve-Factor-App
ozawa
3
160
AOAI で AI アプリを開発する時にまず考えたいこと
mappie_kochi
1
570
PagerDuty×ポストモーテムで築く障害対応文化/Building a culture of incident response with PagerDuty and postmortems
aeonpeople
3
570
Featured
See All Featured
Java REST API Framework Comparison - PWX 2021
mraible
31
8.6k
GraphQLの誤解/rethinking-graphql
sonatard
71
10k
The MySQL Ecosystem @ GitHub 2015
samlambert
251
12k
How to train your dragon (web standard)
notwaldorf
91
6k
Designing Experiences People Love
moore
142
24k
Navigating Team Friction
lara
185
15k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
14
1.4k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
120
52k
Fireside Chat
paigeccino
37
3.4k
Become a Pro
speakerdeck
PRO
28
5.3k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
31
1.2k
Building Flexible Design Systems
yeseniaperezcruz
329
39k
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