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
90
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
59
Writing config files in Ruby
arnvald
0
81
Speaking at RDRC
arnvald
0
75
Read more
arnvald
2
63
Your API is too slow!
arnvald
0
650
The simplest gem you'll ever use
arnvald
0
60
International to global
arnvald
0
52
Patterns, patterns everywhere
arnvald
0
54
Nomadic programmer - Baruco 2014 edition
arnvald
0
120
Other Decks in Technology
See All in Technology
あなたが人生で成功するための5つの普遍的法則 #jawsug #jawsdays2025 / 20250301 HEROZ
yoshidashingo
2
480
Aurora PostgreSQLがCloudWatch Logsに 出力するログの課金を削減してみる #jawsdays2025
non97
1
280
AIエージェント入門
minorun365
PRO
35
20k
Amazon Bedrock 2025 年の熱いアップデート (2025/3 時点)
icoxfog417
PRO
3
430
RaspberryPi CM4(CM5も)面白いぞ!
nonnoise
1
270
x86-64 Assembly Essentials
latte72
4
790
「頑張る」を「楽しむ」に変換する技術
tomoyakitaura
8
1.5k
AIエージェント時代のエンジニアになろう #jawsug #jawsdays2025 / 20250301 Agentic AI Engineering
yoshidashingo
9
4.4k
30→150人のエンジニア組織拡大に伴うアジャイル文化を醸成する役割と取り組みの変化
nagata03
0
410
生成AIがローコードツールになる時代の エンジニアの役割を考える
khwada
0
350
きのこカンファレンス_ランチスポンサーセッション
kabaya
1
290
困難を「一般解」で解く
fujiwara3
9
2.9k
Featured
See All Featured
We Have a Design System, Now What?
morganepeng
51
7.4k
[RailsConf 2023] Rails as a piece of cake
palkan
53
5.3k
Optimizing for Happiness
mojombo
377
70k
4 Signs Your Business is Dying
shpigford
183
22k
A better future with KSS
kneath
238
17k
Docker and Python
trallard
44
3.3k
Practical Orchestrator
shlominoach
186
10k
The MySQL Ecosystem @ GitHub 2015
samlambert
251
12k
Documentation Writing (for coders)
carmenintech
69
4.6k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.5k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
45
9.4k
Stop Working from a Prison Cell
hatefulcrawdad
268
20k
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