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
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
130
International to global
arnvald
0
140
Patterns, patterns everywhere
arnvald
0
160
Nomadic programmer - Baruco 2014 edition
arnvald
0
140
Other Decks in Technology
See All in Technology
FinOps X 2026 Recap from Engineer Side #JapanFinOps
chacco38
0
240
デジタル・デザイン構想 by Sayaka Ishizuka
y150saya
0
180
組織における AI-DLC 実践
askul
0
300
トークン最適化のためのユーザーストーリー分析 / User Story Analysis for Token Optimization
oomatomo
0
160
そのタスクオンスケですか?
poropinai1966
0
120
MySQL & MySQL HeatWave Report - June 2026
freshdaz
0
290
AWS Blocks を触ってみた/first-tach-aws-blocks
fossamagna
2
140
AWS Summit 2026で見えたSIerにとっての Amazon Quickの位置づけ
maf_0521
0
160
Docker Desktop不要の時代が来る? WSL標準の「wslc」で Linuxコンテナを動かしてみた.
ueponx
0
570
From Prompt Engineering to Loop Engineering
shibuiwilliam
1
330
product engineering with qa
nealle
0
140
作る力から、見極める力へ — AI時代に広がるエンジニアの価値と役割
rince
0
400
Featured
See All Featured
RailsConf 2023
tenderlove
30
1.5k
Designing Experiences People Love
moore
143
24k
DBのスキルで生き残る技術 - AI時代におけるテーブル設計の勘所
soudai
PRO
67
56k
What's in a price? How to price your products and services
michaelherold
247
13k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
21
1.5k
Unsuck your backbone
ammeep
672
58k
Building a Modern Day E-commerce SEO Strategy
aleyda
45
9.1k
How to Ace a Technical Interview
jacobian
281
24k
Designing for Performance
lara
611
70k
Deep Space Network (abreviated)
tonyrice
0
220
End of SEO as We Know It (SMX Advanced Version)
ipullrank
3
4.2k
Visualization
eitanlees
152
17k
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