Upgrade to PRO for Only $50/Year—Limited-Time Offer! 🔥
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
140
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
70
Writing config files in Ruby
arnvald
0
120
Speaking at RDRC
arnvald
0
120
Read more
arnvald
2
87
Your API is too slow!
arnvald
0
700
The simplest gem you'll ever use
arnvald
0
89
International to global
arnvald
0
83
Patterns, patterns everywhere
arnvald
0
110
Nomadic programmer - Baruco 2014 edition
arnvald
0
130
Other Decks in Technology
See All in Technology
“決まらない”NSM設計への処方箋 〜ビットキーにおける現実的な指標デザイン事例〜 / A Prescription for "Stuck" NSM Design: Bitkey’s Practical Case Study
bitkey
PRO
1
360
20251127 BigQueryリモート関数で作る、お手軽AIバッチ実行環境
daimatz
0
430
Contract One Engineering Unit 紹介資料
sansan33
PRO
0
9.9k
Security Diaries of an Open Source IAM
ahus1
0
120
モバイルゲーム開発におけるエージェント技術活用への試行錯誤 ~開発効率化へのアプローチの紹介と未来に向けた展望~
qualiarts
0
310
pmconf2025 - データを活用し「価値」へ繋げる
glorypulse
0
460
プロダクトマネージャーが押さえておくべき、ソフトウェア資産とAIエージェント投資効果 / pmconf2025
i35_267
2
360
Bakuraku Engineering Team Deck
layerx
PRO
11
5.8k
バグハンター視点によるサプライチェーンの脆弱性
scgajge12
2
520
Oracle Database@AWS:サービス概要のご紹介
oracle4engineer
PRO
0
120
AI時代の開発フローとともに気を付けたいこと
kkamegawa
0
450
ブロックテーマとこれからの WordPress サイト制作 / Toyama WordPress Meetup Vol.81
torounit
0
330
Featured
See All Featured
Balancing Empowerment & Direction
lara
5
780
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
27k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
25
1.6k
YesSQL, Process and Tooling at Scale
rocio
174
15k
The Hidden Cost of Media on the Web [PixelPalooza 2025]
tammyeverts
1
80
Java REST API Framework Comparison - PWX 2021
mraible
34
9k
We Have a Design System, Now What?
morganepeng
54
7.9k
4 Signs Your Business is Dying
shpigford
186
22k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
162
15k
RailsConf 2023
tenderlove
30
1.3k
BBQ
matthewcrist
89
9.9k
Code Reviewing Like a Champion
maltzj
527
40k
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