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
85
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
78
Speaking at RDRC
arnvald
0
68
Read more
arnvald
2
58
Your API is too slow!
arnvald
0
640
The simplest gem you'll ever use
arnvald
0
53
International to global
arnvald
0
45
Patterns, patterns everywhere
arnvald
0
49
Nomadic programmer - Baruco 2014 edition
arnvald
0
110
Other Decks in Technology
See All in Technology
Site Reliability Engineering on Kubernetes
nwiizo
6
4.4k
Women in Agile
kawaguti
PRO
2
170
Tech Blog執筆のモチベート向上作戦
imamura_ko_0314
0
750
Amazon Location Serviceを使ってラーメンマップを作る
ryder472
2
160
[TechNight #86] Oracle GoldenGate - 23ai 最新情報&プロジェクトからの学び
oracle4engineer
PRO
1
180
データ基盤の成長を加速させる:アイスタイルにおける挑戦と教訓
tsuda7
0
200
プロダクト開発、インフラ、コーポレート、そしてAIとの共通言語としての Terraform / Terraform as a Common Language for Product Development, Infrastructure, Corporate Engineering, and AI
yuyatakeyama
6
1.6k
Kubernetes x k6 で負荷試験基盤を開発して 負荷試験を民主化した話 / Kubernetes x k6
sansan_randd
0
150
個人開発発表 LT - Shinjuku.rb #97
kozy4324
0
100
Active Directory の保護
eurekaberry
1
180
Grid表示のレイアウトで Flow layoutsを使う
cffyoha
1
150
カスタムインストラクションでGitHub Copilotをカスタマイズ!
07jp27
7
720
Featured
See All Featured
Code Reviewing Like a Champion
maltzj
521
39k
Navigating Team Friction
lara
183
15k
No one is an island. Learnings from fostering a developers community.
thoeni
20
3.1k
Stop Working from a Prison Cell
hatefulcrawdad
267
20k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
666
120k
Fireside Chat
paigeccino
34
3.2k
A better future with KSS
kneath
238
17k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
20
2.4k
Faster Mobile Websites
deanohume
305
30k
BBQ
matthewcrist
85
9.4k
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
GraphQLとの向き合い方2022年版
quramy
44
13k
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