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
170
1
Share
Coercion in Ruby
Grzegorz Witek
May 09, 2018
More Decks by Grzegorz Witek
See All by Grzegorz Witek
One Year with Hanami
arnvald
0
84
Writing config files in Ruby
arnvald
0
150
Speaking at RDRC
arnvald
0
160
Read more
arnvald
2
100
Your API is too slow!
arnvald
0
730
The simplest gem you'll ever use
arnvald
0
110
International to global
arnvald
0
120
Patterns, patterns everywhere
arnvald
0
140
Nomadic programmer - Baruco 2014 edition
arnvald
0
130
Other Decks in Technology
See All in Technology
40代からのアウトプット ― 経験は価値ある学びに変わる / 20260404 Naoki Takahashi
shift_evolve
PRO
4
760
Network Firewall Proxyで 自前プロキシを消し去ることができるのか
gusandayo
0
170
不確実性と戦いながら見積もりを作成するプロセス/mitsumori-process
hirodragon112
1
180
AIにより大幅に強化された AWS Transform Customを触ってみる
0air
0
280
最大のアウトプット術は問題を作ること
ryoaccount
0
280
Cursor Subagentsはいいぞ
yug1224
2
140
Tour of Agent Protocols: MCP, A2A, AG-UI, A2UI with ADK
meteatamel
0
200
TUNA Camp 2026 京都Stage ヒューリスティックアルゴリズム入門
terryu16
0
670
スケーリングを封じられたEC2を救いたい
senseofunity129
0
140
来期の評価で変えようと思っていること 〜AI時代に変わること・変わらないこと〜
estie
0
130
Embeddings : Symfony AI en pratique
lyrixx
0
450
CloudFrontのHost Header転送設定でパケットの中身はどう変わるのか?
nagisa53
1
250
Featured
See All Featured
How to Align SEO within the Product Triangle To Get Buy-In & Support - #RIMC
aleyda
1
1.5k
Crafting Experiences
bethany
1
100
How STYLIGHT went responsive
nonsquared
100
6k
Mind Mapping
helmedeiros
PRO
1
140
Building Experiences: Design Systems, User Experience, and Full Site Editing
marktimemedia
0
470
世界の人気アプリ100個を分析して見えたペイウォール設計の心得
akihiro_kokubo
PRO
68
38k
Testing 201, or: Great Expectations
jmmastey
46
8.1k
コードの90%をAIが書く世界で何が待っているのか / What awaits us in a world where 90% of the code is written by AI
rkaga
61
43k
Tell your own story through comics
letsgokoyo
1
880
Building a Modern Day E-commerce SEO Strategy
aleyda
45
9k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
249
1.3M
Dominate Local Search Results - an insider guide to GBP, reviews, and Local SEO
greggifford
PRO
0
130
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