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
130
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
67
Writing config files in Ruby
arnvald
0
120
Speaking at RDRC
arnvald
0
120
Read more
arnvald
2
81
Your API is too slow!
arnvald
0
700
The simplest gem you'll ever use
arnvald
0
85
International to global
arnvald
0
78
Patterns, patterns everywhere
arnvald
0
96
Nomadic programmer - Baruco 2014 edition
arnvald
0
130
Other Decks in Technology
See All in Technology
AIで急増した生産「量」の荒波をCodeRabbitで乗りこなそう
moongift
PRO
0
540
開発者が知っておきたい複雑さの正体/where-the-complexity-comes-from
hanhan1978
1
280
MCP サーバーの基礎から実践レベルの知識まで
azukiazusa1
18
9k
SREのキャリアから経営に近づく - Enterprise Risk Managementを基に -
shonansurvivors
1
740
猫でもわかるAmazon Q Developer CLI 解体新書
kentapapa
1
320
激動の時代を爆速リチーミングで乗り越えろ
sansantech
PRO
1
260
AIがコードを書いてくれるなら、新米エンジニアは何をする? / komekaigi2025
nkzn
25
17k
20251102 WordCamp Kansai 2025
chiilog
1
550
LLM APIを2年間本番運用して苦労した話
ivry_presentationmaterials
10
8.3k
QAEが生成AIと越える、ソフトウェア開発の境界線
rinchsan
0
260
アノテーション作業書作成のGood Practice
cierpa0905
PRO
1
400
NOT A HOTEL SOFTWARE DECK (2025/11/06)
notahotel
0
3.1k
Featured
See All Featured
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
359
30k
Rails Girls Zürich Keynote
gr2m
95
14k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
27k
A Modern Web Designer's Workflow
chriscoyier
697
190k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
31
9.7k
A Tale of Four Properties
chriscoyier
161
23k
Bash Introduction
62gerente
615
210k
Statistics for Hackers
jakevdp
799
220k
Principles of Awesome APIs and How to Build Them.
keavy
127
17k
How Fast Is Fast Enough? [PerfNow 2025]
tammyeverts
2
270
Code Review Best Practice
trishagee
72
19k
The MySQL Ecosystem @ GitHub 2015
samlambert
251
13k
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