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
89
Writing config files in Ruby
arnvald
0
150
Speaking at RDRC
arnvald
0
160
Read more
arnvald
2
110
Your API is too slow!
arnvald
0
730
The simplest gem you'll ever use
arnvald
0
120
International to global
arnvald
0
120
Patterns, patterns everywhere
arnvald
0
140
Nomadic programmer - Baruco 2014 edition
arnvald
0
140
Other Decks in Technology
See All in Technology
Keeping Ruby Running on Cygwin
fd0
0
180
EMから幅を広げるために最近挑戦していること / Recent challenges I'm undertaking to expand my horizons beyond EM
hiro_torii
1
140
Route 53 Global Resolver で高額課金発生!
otanikohei2023
0
120
20年前の「OSS革命」に学ぶ AI時代の生存戦略
samakada
0
490
国内外の生成AIセキュリティの最新動向 & AIガードレール製品「chakoshi」のご紹介 / Latest Trends in Generative AI Security (Domestic & International) & Introduction to AI Guardrail Product "chakoshi"
nttcom
4
1.5k
AI: Making Admin and Users, Lives Better
kbmsg
0
120
Good Enough Types: Heuristic Type Inference for Ruby
riseshia
1
320
UIライブラリに依存しすぎないReact Native設計を目指して
grandbig
0
150
Oracle AI Database@AWS:サービス概要のご紹介
oracle4engineer
PRO
4
2.4k
[OpsJAWS 40]リリースしたら終わり、じゃなかった。セキュリティ空白期間をAWS Security Agentで埋める
sh_fk2
3
250
CloudTrail を見つめ直してみる
kazzpapa3
1
120
AzureのIaC管理からログ調査まで、随所に役立つSkillsとCustom-Instructions / Boosting IaC and Log Analysis with Skills
aeonpeople
0
280
Featured
See All Featured
Lessons Learnt from Crawling 1000+ Websites
charlesmeaden
PRO
1
1.2k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
49
9.9k
B2B Lead Gen: Tactics, Traps & Triumph
marketingsoph
0
110
How Fast Is Fast Enough? [PerfNow 2025]
tammyeverts
3
540
Leo the Paperboy
mayatellez
7
1.7k
Avoiding the “Bad Training, Faster” Trap in the Age of AI
tmiket
0
130
From π to Pie charts
rasagy
0
170
Pawsitive SEO: Lessons from My Dog (and Many Mistakes) on Thriving as a Consultant in the Age of AI
davidcarrasco
0
120
Breaking role norms: Why Content Design is so much more than writing copy - Taylor Woolridge
uxyall
0
260
New Earth Scene 8
popppiees
3
2.1k
Applied NLP in the Age of Generative AI
inesmontani
PRO
4
2.2k
Rails Girls Zürich Keynote
gr2m
96
14k
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