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
78
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
72
Speaking at RDRC
arnvald
0
62
Read more
arnvald
2
50
Your API is too slow!
arnvald
0
630
The simplest gem you'll ever use
arnvald
0
46
International to global
arnvald
0
37
Patterns, patterns everywhere
arnvald
0
39
Nomadic programmer - Baruco 2014 edition
arnvald
0
110
Other Decks in Technology
See All in Technology
非機能品質を作り込むための実践アーキテクチャ
knih
2
660
re:Invent 2024 Innovation Talks(NET201)で語られた大切なこと
shotashiratori
0
300
ゼロから創る横断SREチーム 挑戦と進化の軌跡
rvirus0817
2
260
NilAway による静的解析で「10 億ドル」を節約する #kyotogo / Kyoto Go 56th
ytaka23
3
370
AWS re:Invent 2024で発表された コードを書く開発者向け機能について
maruto
0
180
DevOps視点でAWS re:invent2024の新サービス・アプデを振り返ってみた
oshanqq
0
180
私なりのAIのご紹介 [2024年版]
qt_luigi
1
120
レンジャーシステムズ | 会社紹介(採用ピッチ)
rssytems
0
150
Fanstaの1年を大解剖! 一人SREはどこまでできるのか!?
syossan27
2
160
複雑性の高いオブジェクト編集に向き合う: プラガブルなReactフォーム設計
righttouch
PRO
0
110
Jetpack Composeで始めるServer Cache State
ogaclejapan
2
160
統計データで2024年の クラウド・インフラ動向を眺める
ysknsid25
2
830
Featured
See All Featured
Embracing the Ebb and Flow
colly
84
4.5k
Reflections from 52 weeks, 52 projects
jeffersonlam
347
20k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
132
33k
Facilitating Awesome Meetings
lara
50
6.1k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
2
160
Rebuilding a faster, lazier Slack
samanthasiow
79
8.7k
Writing Fast Ruby
sferik
628
61k
Fantastic passwords and where to find them - at NoRuKo
philnash
50
2.9k
A better future with KSS
kneath
238
17k
How to train your dragon (web standard)
notwaldorf
88
5.7k
Into the Great Unknown - MozCon
thekraken
33
1.5k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
6
510
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