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
63
Writing config files in Ruby
arnvald
0
100
Speaking at RDRC
arnvald
0
95
Read more
arnvald
2
72
Your API is too slow!
arnvald
0
670
The simplest gem you'll ever use
arnvald
0
68
International to global
arnvald
0
63
Patterns, patterns everywhere
arnvald
0
76
Nomadic programmer - Baruco 2014 edition
arnvald
0
120
Other Decks in Technology
See All in Technology
UIテスト自動化サポート- Testbed for XCUIAutomation practice
notoroid
0
120
低レイヤを知りたいPHPerのためのCコンパイラ作成入門 完全版 / Building a C Compiler for PHPers Who Want to Dive into Low-Level Programming - Expanded
tomzoh
4
2.7k
初めてのAzure FunctionsをClaude Codeで作ってみた / My first Azure Functions using Claude Code
hideakiaoyagi
1
200
Azure AI Foundryでマルチエージェントワークフロー
seosoft
0
160
菸酒生在 LINE Taiwan 的後端雙刀流
line_developers_tw
PRO
0
1.1k
TechLION vol.41~MySQLユーザ会のほうから来ました / techlion41_mysql
sakaik
0
160
PostgreSQL 18 cancel request key長の変更とRailsへの関連
yahonda
0
110
Абьюзим random_bytes(). Фёдор Кулаков, разработчик Lamoda Tech
lamodatech
0
310
Amazon S3標準/ S3 Tables/S3 Express One Zoneを使ったログ分析
shigeruoda
3
420
原則から考える保守しやすいComposable関数設計
moriatsushi
3
510
PHPでWebブラウザのレンダリングエンジンを実装する
dip_tech
PRO
0
180
rubygem開発で鍛える設計力
joker1007
1
170
Featured
See All Featured
Code Reviewing Like a Champion
maltzj
524
40k
The Invisible Side of Design
smashingmag
299
51k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
PRO
181
53k
Keith and Marios Guide to Fast Websites
keithpitt
411
22k
Mobile First: as difficult as doing things right
swwweet
223
9.7k
Being A Developer After 40
akosma
90
590k
Learning to Love Humans: Emotional Interface Design
aarron
273
40k
Into the Great Unknown - MozCon
thekraken
39
1.9k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
53
2.8k
Rails Girls Zürich Keynote
gr2m
94
14k
Optimizing for Happiness
mojombo
379
70k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
8
670
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