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
120
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
65
Writing config files in Ruby
arnvald
0
120
Speaking at RDRC
arnvald
0
120
Read more
arnvald
2
80
Your API is too slow!
arnvald
0
690
The simplest gem you'll ever use
arnvald
0
82
International to global
arnvald
0
77
Patterns, patterns everywhere
arnvald
0
93
Nomadic programmer - Baruco 2014 edition
arnvald
0
120
Other Decks in Technology
See All in Technology
コンパウンド組織のCRE #cre_meetup
layerx
PRO
0
220
JSConf JPのwebsiteをGatsbyからNext.jsに移行した話 - Next.jsの多言語静的サイトと課題
leko
2
180
AIとともに歩んでいくデザイナーの役割の変化
lycorptech_jp
PRO
0
800
AI駆動で進める依存ライブラリ更新 ─ Vue プロジェクトの品質向上と開発スピード改善の実践録
sayn0
1
110
「最速」で Gemini CLI を使いこなそう! 〜Cloud Shell/Cloud Run の活用〜 / The Fastest Way to Master the Gemini CLI — with Cloud Shell and Cloud Run
aoto
PRO
0
150
AI AgentをLangflowでサクッと作って、1日働かせてみた!
yano13
1
120
SCONE - 動画配信の帯域を最適化する新プロトコル
kazuho
1
310
現場データから見える、開発生産性の変化コード生成AI導入・運用のリアル〜 / Changes in Development Productivity and Operational Challenges Following the Introduction of Code Generation AI
nttcom
1
440
今この時代に技術とどう向き合うべきか
gree_tech
PRO
2
2.1k
Databricks AI/BI Genie の「値ディクショナリー」をAmazonの奥地(S3)まで見に行く
kameitomohiro
1
380
HonoとJSXを使って管理画面をサクッと型安全に作ろう
diggymo
0
160
Building a cloud native business on open source
lizrice
0
170
Featured
See All Featured
Statistics for Hackers
jakevdp
799
220k
Faster Mobile Websites
deanohume
310
31k
Documentation Writing (for coders)
carmenintech
75
5.1k
Product Roadmaps are Hard
iamctodd
PRO
55
11k
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.5k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
12
1.2k
StorybookのUI Testing Handbookを読んだ
zakiyama
31
6.2k
GraphQLの誤解/rethinking-graphql
sonatard
73
11k
The Straight Up "How To Draw Better" Workshop
denniskardys
238
140k
Become a Pro
speakerdeck
PRO
29
5.6k
Leading Effective Engineering Teams in the AI Era
addyosmani
7
570
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
10
610
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