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
73
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
56
Writing config files in Ruby
arnvald
0
67
Speaking at RDRC
arnvald
0
57
Read more
arnvald
2
45
Your API is too slow!
arnvald
0
620
The simplest gem you'll ever use
arnvald
0
42
International to global
arnvald
0
32
Patterns, patterns everywhere
arnvald
0
33
Nomadic programmer - Baruco 2014 edition
arnvald
0
110
Other Decks in Technology
See All in Technology
Terraform Stacks入門 #HashiTalks
msato
0
360
あなたの知らない Function.prototype.toString() の世界
mizdra
PRO
2
430
静的解析で実現した効率的なi18n対応の仕組みづくり
minako__ph
1
120
OCI Network Firewall 概要
oracle4engineer
PRO
0
4.2k
アプリエンジニアのためのGraphQL入門.pdf
spycwolf
0
110
Introduction to Works of ML Engineer in LY Corporation
lycorp_recruit_jp
0
150
適材適所の技術選定 〜GraphQL・REST API・tRPC〜 / Optimal Technology Selection
kakehashi
1
720
ExaDB-D dbaascli で出来ること
oracle4engineer
PRO
0
3.9k
ノーコードデータ分析ツールで体験する時系列データ分析超入門
negi111111
0
430
DynamoDB でスロットリングが発生したとき/when_throttling_occurs_in_dynamodb_short
emiki
0
270
EventHub Startup CTO of the year 2024 ピッチ資料
eventhub
0
130
Application Development WG Intro at AppDeveloperCon
salaboy
0
200
Featured
See All Featured
A better future with KSS
kneath
238
17k
Code Review Best Practice
trishagee
64
17k
The Power of CSS Pseudo Elements
geoffreycrofte
73
5.3k
Teambox: Starting and Learning
jrom
133
8.8k
The MySQL Ecosystem @ GitHub 2015
samlambert
250
12k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
356
29k
Navigating Team Friction
lara
183
14k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
280
13k
Optimizing for Happiness
mojombo
376
70k
Rails Girls Zürich Keynote
gr2m
94
13k
How GitHub (no longer) Works
holman
310
140k
Building Your Own Lightsaber
phodgson
103
6.1k
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