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
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
Grzegorz Witek
May 09, 2018
Technology
190
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Coercion in Ruby
Grzegorz Witek
May 09, 2018
More Decks by Grzegorz Witek
See All by Grzegorz Witek
One Year with Hanami
arnvald
0
110
Writing config files in Ruby
arnvald
0
180
Speaking at RDRC
arnvald
0
180
Read more
arnvald
2
140
Your API is too slow!
arnvald
0
760
The simplest gem you'll ever use
arnvald
0
150
International to global
arnvald
0
150
Patterns, patterns everywhere
arnvald
0
170
Nomadic programmer - Baruco 2014 edition
arnvald
0
150
Other Decks in Technology
See All in Technology
[RSJ26] Building a VLA Model Based on Self-Distilled Classification
keio_smilab
PRO
0
150
Bet AI Day 2026丨Agentは、「金融」という巨大産業の何を変えられるのか
layerx
PRO
0
380
When Token Pruning is Worse than Random: Understanding Visual Token Information in VLLMs
sansantech
PRO
0
220
APIセキュリティを組織で実現するには~注力する点と設計・実装に入れたい対策~
riiimparm
3
840
AIレビュー時代に必要なのは、SLOで引く撤退ライン
nobuoooo
0
160
本番に近いテストをもっと手軽に - Postmanで広がるAPIテストの世界 / Expanding the World of API Testing with Postman
yokawasa
1
200
「ミスを許さない手順書」を作ってみた 〜 個人的にはこれ以上できることはあまりなさそう/20260827-ssmjp-operation-procedure-update
opelab
17
17k
Nav2、Nav3 ... はたまた自作?〜 作って理解する Nav3 の設計意図 〜 / Nav2, Nav3 ... or Build Your Own? — Understanding Nav3's design intent by building it from scratch
yanzm
0
120
[2026-08-25]AIによる自動化と人の介入、その狭間で揺れる信頼性についての俺の思いを、波よ聞いてくれ
tosite
0
120
1.5時間を無駄にして学んだwsl2におけるaptとsnapの選択と仕組み
yosaka0123
0
480
Bet AI Day 2026丨バクラク Autopilot、業務システムの再設計
layerx
PRO
1
620
Jetpack Compose で挑む新聞紙面UI ─ 複合ジェスチャー・ポリゴン記事領域・適応的ページ構成という3つの壁/droidkaigi2026
nikkei_engineer_recruiting
0
110
Featured
See All Featured
Neural Spatial Audio Processing for Sound Field Analysis and Control
skoyamalab
0
430
Crafting Experiences
bethany
1
270
Breaking role norms: Why Content Design is so much more than writing copy - Taylor Woolridge
uxyall
1
390
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
46
3k
Jamie Indigo - Trashchat’s Guide to Black Boxes: Technical SEO Tactics for LLMs
techseoconnect
PRO
0
640
Building Better People: How to give real-time feedback that sticks.
wjessup
370
20k
Public Speaking Without Barfing On Your Shoes - THAT 2023
reverentgeek
1
550
Navigating the Design Leadership Dip - Product Design Week Design Leaders+ Conference 2024
apolaine
2
410
State of Search Keynote: SEO is Dead Long Live SEO
ryanjones
0
260
Become a Pro
speakerdeck
PRO
31
6.2k
The agentic SEO stack - context over prompts
schlessera
0
880
Done Done
chrislema
186
16k
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