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
1
150
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
80
Writing config files in Ruby
arnvald
0
130
Speaking at RDRC
arnvald
0
140
Read more
arnvald
2
98
Your API is too slow!
arnvald
0
720
The simplest gem you'll ever use
arnvald
0
110
International to global
arnvald
0
110
Patterns, patterns everywhere
arnvald
0
130
Nomadic programmer - Baruco 2014 edition
arnvald
0
130
Other Decks in Technology
See All in Technology
2026年のAIエージェント構築はどうなる?
minorun365
11
2.4k
論文検索を日本語でできるアプリを作ってみた
sailen2
0
120
Agentic Codingの実践とチームで導入するための工夫
lycorptech_jp
PRO
0
180
オンプレとGoogle Cloudを安全に繋ぐための、セキュア通信の勘所
waiwai2111
1
120
Claude Codeと駆け抜ける 情報収集と実践録
sontixyou
1
1.1k
Devinを導入したら予想外の人たちに好評だった
tomuro
0
170
LINEヤフーにおけるAI駆動開発組織のプロデュース施策
lycorptech_jp
PRO
0
160
ブログの作成に音声AIツールを使って音声入力しようとした話
smt7174
1
190
opsmethod第1回_アラート調査の自動化にむけて
yamatook
0
300
GoとWasmでつくる軽量ブラウザUI
keyl0ve
0
140
インシデント対応入門
grimoh
7
5.2k
NW構成図の自動描画は何が難しいのか?/netdevnight3
corestate55
2
470
Featured
See All Featured
技術選定の審美眼(2025年版) / Understanding the Spiral of Technologies 2025 edition
twada
PRO
117
110k
Intergalactic Javascript Robots from Outer Space
tanoku
273
27k
Designing for Performance
lara
611
70k
Evolving SEO for Evolving Search Engines
ryanjones
0
140
How to build an LLM SEO readiness audit: a practical framework
nmsamuel
1
660
Build your cross-platform service in a week with App Engine
jlugia
234
18k
Keith and Marios Guide to Fast Websites
keithpitt
413
23k
30 Presentation Tips
portentint
PRO
1
240
How To Stay Up To Date on Web Technology
chriscoyier
791
250k
Product Roadmaps are Hard
iamctodd
PRO
55
12k
Getting science done with accelerated Python computing platforms
jacobtomlinson
2
130
Bioeconomy Workshop: Dr. Julius Ecuru, Opportunities for a Bioeconomy in West Africa
akademiya2063
PRO
1
66
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