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
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
Grzegorz Witek
May 09, 2018
Technology
170
1
Share
Coercion in Ruby
Grzegorz Witek
May 09, 2018
More Decks by Grzegorz Witek
See All by Grzegorz Witek
One Year with Hanami
arnvald
0
92
Writing config files in Ruby
arnvald
0
150
Speaking at RDRC
arnvald
0
160
Read more
arnvald
2
120
Your API is too slow!
arnvald
0
740
The simplest gem you'll ever use
arnvald
0
120
International to global
arnvald
0
130
Patterns, patterns everywhere
arnvald
0
150
Nomadic programmer - Baruco 2014 edition
arnvald
0
140
Other Decks in Technology
See All in Technology
Fラン学生が考える、AI時代のデザインに執着した突破口
husengs7
1
220
生成AI時代に信頼性をどう保ち続けるか - Policy as Code の実践
akitok_
1
510
その英語学習、AWSで代替できませんか?
suzutatsu
1
140
LookerとADKで作る社内AIエージェント
chanyou0311
0
270
10サービス以上のメール到達率改善を地道に継続的に進めている話 / Continue to improve email delivery rates across multiple services
yamaguchitk333
6
2.2k
ワールドカフェ再び、そしてゴール・ルール・ロール・ツール / World Café Revisited, and the Goals-Rules-Roles-Tools
ks91
PRO
0
180
Loadbalancing exporter internals
ymotongpoo
1
110
R&D 祭 2024 アニメエフェクト作成の効率化
olmdrd
PRO
0
100
AWS運用におけるAI Agent活用術 / JAWS-UG 神戸 #11 LT大会
genda
1
310
20260515 ⾃分のアカウントとプライバシーを守る認証と認可の話〜利⽤者向け〜
oidfj
0
790
業務に残された「良くない型」で考える「TypeScriptの難しさ」
sajikix
2
630
React Compiler導入から21ヶ月、いま始めるならこうやる
astatsuya
2
270
Featured
See All Featured
Efficient Content Optimization with Google Search Console & Apps Script
katarinadahlin
PRO
1
550
We Are The Robots
honzajavorek
0
230
Visual Storytelling: How to be a Superhuman Communicator
reverentgeek
2
530
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.7k
Neural Spatial Audio Processing for Sound Field Analysis and Control
skoyamalab
0
300
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
4.3k
Building a Scalable Design System with Sketch
lauravandoore
463
34k
Self-Hosted WebAssembly Runtime for Runtime-Neutral Checkpoint/Restore in Edge–Cloud Continuum
chikuwait
0
530
So, you think you're a good person
axbom
PRO
2
2k
How To Speak Unicorn (iThemes Webinar)
marktimemedia
1
460
The AI Revolution Will Not Be Monopolized: How open-source beats economies of scale, even for LLMs
inesmontani
PRO
3
3.4k
Six Lessons from altMBA
skipperchong
29
4.2k
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