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
180
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
100
Writing config files in Ruby
arnvald
0
170
Speaking at RDRC
arnvald
0
170
Read more
arnvald
2
130
Your API is too slow!
arnvald
0
750
The simplest gem you'll ever use
arnvald
0
140
International to global
arnvald
0
140
Patterns, patterns everywhere
arnvald
0
160
Nomadic programmer - Baruco 2014 edition
arnvald
0
150
Other Decks in Technology
See All in Technology
なぜ、あなたのエージェントは言うことを聞かないのか
segavvy
1
580
全社でのソフトウェアサプライチェーン攻撃対策をやってみた with Takumi Guard
z63d
0
330
Escolhendo LLMs na Prática: Lições Reais em Busca Agêntica no Mercado Livre —TDC 2026 Floripa
jpbonson
0
110
AIがAPIを書く時代に、私たちは何を設計すべきか
nagix
0
160
Webの技術とガジェットで子どもも大人も楽しめるワクワク体験を提供する / Qiita Tech Festa Day 2026
you
PRO
1
310
基調講演:人とAIをつなぐIoTの今と未来 ー 「フィジカル」と「デジタル」が出会うその先へ【SORACOM Discovery 2026】
soracom
PRO
0
360
AIツールを導入しても生産性はあがらない? カオナビが直面した 3つの壁と乗り越え方。/ Overcoming 3 Barriers to AI-Driven Productivity at kaonavi
kaonavi
0
460
実践が先生だった— 新卒サーバーエンジニア1年目のリアル
mixi_engineers
PRO
0
200
データ活用研修 問いの発見と仮説構築【MIXI 26新卒技術研修】
mixi_engineers
PRO
1
670
新しい SLO が良い感じにハマっている話
z63d
0
120
脱Jenkins、インターン生が挑んだCIツールGitHubActions移行
mixi_engineers
PRO
1
260
BigQuery を検索ソースとした AI Agent の作り方って 〇〇 通りあんねん
satohjohn
0
140
Featured
See All Featured
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
49
3.5k
Speed Design
sergeychernyshev
33
2k
How to Grow Your eCommerce with AI & Automation
katarinadahlin
PRO
1
230
The Power of CSS Pseudo Elements
geoffreycrofte
82
6.5k
My Coaching Mixtape
mlcsv
0
180
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
133
19k
Everyday Curiosity
cassininazir
0
270
Principles of Awesome APIs and How to Build Them.
keavy
128
18k
The MySQL Ecosystem @ GitHub 2015
samlambert
251
13k
How to Ace a Technical Interview
jacobian
281
24k
Fireside Chat
paigeccino
42
4k
Product Roadmaps are Hard
iamctodd
55
12k
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