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
71
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
51
Writing config files in Ruby
arnvald
0
67
Speaking at RDRC
arnvald
0
51
Read more
arnvald
2
44
Your API is too slow!
arnvald
0
590
The simplest gem you'll ever use
arnvald
0
40
International to global
arnvald
0
32
Patterns, patterns everywhere
arnvald
0
30
Nomadic programmer - Baruco 2014 edition
arnvald
0
110
Other Decks in Technology
See All in Technology
フルカイテン株式会社 採用資料
fullkaiten
0
32k
Tricentisにおけるテスト自動化へのAI活用ご紹介/20240910Shunsuke Katakura
shift_evolve
0
200
再考 アクターモデル/ reconsider actor model
ytake
0
360
不動産 x AIことはじめ~データの真価を拓くために
estie
0
110
DroidKaigi 2024 たすけて!ViewModel
mhidaka
5
950
AIで変わるテスト自動化:最新ツールの多様なアプローチ/ 20240910 Takahiro Kaneyama
shift_evolve
0
240
JTCや セキュリティチェックリストが夢の跡
nikinusu
1
660
20240912 JJUGナイトセミナー
mii1004
0
140
OSTという文化を組織に根付かせてみた
sansantech
PRO
2
390
OCI で始める!! Red Hat OpenShift / Get Started OpenShift on OCI
oracle4engineer
PRO
1
180
QAに対する超個人的な解釈 / Personal Take on QA
toma_sm
1
120
言葉は感情の近似値である。その感情と言葉の誤差を最小化しよう ~コミュニケーションにおけるアナログ/デジタル変換の課題に立ち向かう~
nktamago
0
230
Featured
See All Featured
Rails Girls Zürich Keynote
gr2m
93
13k
The Brand Is Dead. Long Live the Brand.
mthomps
53
38k
Unsuck your backbone
ammeep
667
57k
5 minutes of I Can Smell Your CMS
philhawksworth
202
19k
Product Roadmaps are Hard
iamctodd
PRO
48
10k
Build The Right Thing And Hit Your Dates
maggiecrowley
30
2.3k
It's Worth the Effort
3n
182
27k
Adopting Sorbet at Scale
ufuk
73
8.9k
The Pragmatic Product Professional
lauravandoore
31
6.2k
Raft: Consensus for Rubyists
vanstee
135
6.5k
Fireside Chat
paigeccino
31
2.9k
The Language of Interfaces
destraynor
153
23k
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