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
110
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
63
Writing config files in Ruby
arnvald
0
100
Speaking at RDRC
arnvald
0
95
Read more
arnvald
2
72
Your API is too slow!
arnvald
0
670
The simplest gem you'll ever use
arnvald
0
68
International to global
arnvald
0
63
Patterns, patterns everywhere
arnvald
0
76
Nomadic programmer - Baruco 2014 edition
arnvald
0
120
Other Decks in Technology
See All in Technology
本当に使える?AutoUpgrade の新機能を実践検証してみた
oracle4engineer
PRO
1
130
プロダクトエンジニアリング組織への歩み、その現在地 / Our journey to becoming a product engineering organization
hiro_torii
0
120
強化されたAmazon Location Serviceによる新機能と開発者体験
dayjournal
2
170
あなたの声を届けよう! 女性エンジニア登壇の意義とアウトプット実践ガイド #wttjp / Call for Your Voice
kondoyuko
2
270
菸酒生在 LINE Taiwan 的後端雙刀流
line_developers_tw
PRO
0
1.1k
OAuth/OpenID Connectで実現するMCPのセキュアなアクセス管理
kuralab
5
910
AWS アーキテクチャ作図入門/aws-architecture-diagram-101
ma2shita
29
9.6k
原則から考える保守しやすいComposable関数設計
moriatsushi
3
510
Amazon S3標準/ S3 Tables/S3 Express One Zoneを使ったログ分析
shigeruoda
3
410
TechLION vol.41~MySQLユーザ会のほうから来ました / techlion41_mysql
sakaik
0
160
Observability в PHP без боли. Олег Мифле, тимлид Altenar
lamodatech
0
310
2年でここまで成長!AWSで育てたAI Slack botの軌跡
iwamot
PRO
4
550
Featured
See All Featured
Being A Developer After 40
akosma
90
590k
StorybookのUI Testing Handbookを読んだ
zakiyama
30
5.8k
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
252
21k
Designing for Performance
lara
609
69k
Done Done
chrislema
184
16k
For a Future-Friendly Web
brad_frost
179
9.8k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
248
1.3M
Product Roadmaps are Hard
iamctodd
PRO
53
11k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
34
3k
Intergalactic Javascript Robots from Outer Space
tanoku
271
27k
Building Better People: How to give real-time feedback that sticks.
wjessup
367
19k
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