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
97
Writing config files in Ruby
arnvald
0
160
Speaking at RDRC
arnvald
0
170
Read more
arnvald
2
130
Your API is too slow!
arnvald
0
740
The simplest gem you'll ever use
arnvald
0
130
International to global
arnvald
0
130
Patterns, patterns everywhere
arnvald
0
150
Nomadic programmer - Baruco 2014 edition
arnvald
0
140
Other Decks in Technology
See All in Technology
AI Engineering Summit Tokyo 2026 AIの前に、やることがある 〜医療データ企業の4フェーズ〜
dtaniwaki
0
2.4k
やさしいA2A入門
minorun365
PRO
10
1.5k
Oracle AI Database@Azure:サービス概要のご紹介
oracle4engineer
PRO
6
1.9k
2026TECHFRESH畢業分享會 - Lightning Talk - 資料也要 CI/CD? 用 Airbyte 自動化資料同步
line_developers_tw
PRO
0
590
「速く作る」から「正しく作る」へ ─ 生成AI時代の開発フロー改革の ロードマップと実行 ─
starfish719
0
9.5k
AI活用を推進するために ファインディが下した、一つの小さな決断
starfish719
0
290
Disciplined Vibes: Scaling AI-Assisted Engineering
sheharyar
0
120
AAIFに入ってみた ~内から見えるコミュニティ動向~
sato4
0
120
日本 Fintech 未来予測レポート 2027〜2028年(オリジナル版)
8maki
0
610
AmazonRoute 53ではじめてのドメイン取得!HTTPS化までの道のりを整理してみた
usanchuu
3
120
AI駆動開発が変える、大規模開発の前提 ーHuman in the Loop から Human on the Loop へ / AIE2026
visional_engineering_and_design
30
23k
失敗を資産に変えるClaude Code
shinyasaita
0
210
Featured
See All Featured
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.8k
Building Experiences: Design Systems, User Experience, and Full Site Editing
marktimemedia
0
530
Exploring anti-patterns in Rails
aemeredith
3
400
What Being in a Rock Band Can Teach Us About Real World SEO
427marketing
0
250
The Illustrated Guide to Node.js - THAT Conference 2024
reverentgeek
1
380
For a Future-Friendly Web
brad_frost
183
10k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
666
130k
Navigating Weather and Climate Data
rabernat
0
220
The World Runs on Bad Software
bkeepers
PRO
72
12k
End of SEO as We Know It (SMX Advanced Version)
ipullrank
3
4.2k
Imperfection Machines: The Place of Print at Facebook
scottboms
270
14k
Designing Powerful Visuals for Engaging Learning
tmiket
1
410
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