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
170
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
82
Writing config files in Ruby
arnvald
0
140
Speaking at RDRC
arnvald
0
150
Read more
arnvald
2
99
Your API is too slow!
arnvald
0
720
The simplest gem you'll ever use
arnvald
0
110
International to global
arnvald
0
110
Patterns, patterns everywhere
arnvald
0
130
Nomadic programmer - Baruco 2014 edition
arnvald
0
130
Other Decks in Technology
See All in Technology
AlloyDB 奮闘記
hatappi
0
150
生成AIで速度と品質を両立する、QAエンジニア・開発者連携のAI協調型テストプロセス
shota_kusaba
0
200
僕、S3 シンプルって名前だけど全然シンプルじゃありません よろしくお願いします
yama3133
1
230
20260311 技術SWG活動報告(デジタルアイデンティティ人材育成推進WG Ph2 活動報告会)
oidfj
0
370
CyberAgentの生成AI戦略 〜変わるものと変わらないもの〜
katayan
0
270
詳解 強化学習 / In-depth Guide to Reinforcement Learning
prinlab
0
300
ガバメントクラウドにおけるAWSの長期継続割引について
takeda_h
2
5.3k
アーキテクチャモダナイゼーションを実現する組織
satohjohn
1
1.1k
OpenClaw を Amazon Lightsail で動かす理由
uechishingo
0
200
WebアクセシビリティをCI/CDで担保する ― axe DevTools × Playwright C#実践ガイド
tomokusaba
2
180
夢の無限スパゲッティ製造機 #phperkaigi
o0h
PRO
0
160
「通るまでRe-run」から卒業!落ちないテストを書く勘所
asumikam
1
150
Featured
See All Featured
Impact Scores and Hybrid Strategies: The future of link building
tamaranovitovic
0
230
AI Search: Where Are We & What Can We Do About It?
aleyda
0
7.1k
GraphQLの誤解/rethinking-graphql
sonatard
75
11k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
133
19k
Organizational Design Perspectives: An Ontology of Organizational Design Elements
kimpetersen
PRO
1
640
It's Worth the Effort
3n
188
29k
The Art of Programming - Codeland 2020
erikaheidi
57
14k
Efficient Content Optimization with Google Search Console & Apps Script
katarinadahlin
PRO
1
410
Navigating Algorithm Shifts & AI Overviews - #SMXNext
aleyda
1
1.2k
Bioeconomy Workshop: Dr. Julius Ecuru, Opportunities for a Bioeconomy in West Africa
akademiya2063
PRO
1
74
The browser strikes back
jonoalderson
0
810
Darren the Foodie - Storyboard
khoart
PRO
3
2.9k
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