Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Coercion in Ruby
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
Grzegorz Witek
May 09, 2018
Technology
200
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Coercion in Ruby
Grzegorz Witek
May 09, 2018
More Decks by Grzegorz Witek
See All by Grzegorz Witek
One Year with Hanami
arnvald
0
130
Writing config files in Ruby
arnvald
0
190
Speaking at RDRC
arnvald
0
190
Read more
arnvald
2
150
Your API is too slow!
arnvald
0
790
The simplest gem you'll ever use
arnvald
0
170
International to global
arnvald
0
170
Patterns, patterns everywhere
arnvald
0
180
Nomadic programmer - Baruco 2014 edition
arnvald
0
160
Other Decks in Technology
See All in Technology
Context Ontology Accelerator 入門 (業務データの意味を、AI エージェントに渡す)
inariku
PRO
3
610
Active Directory と Entra ID のむむむな関係
murachiakira
PRO
4
1.7k
並列度を下げたら速くなった — go build の重複コンパイルを実測で見る
tomoaki25
2
1k
分析作業をどこまで任せるか自分で決められる AIネイティブBI『Golden Analytics』が 良さげな話 /golden-analytics-is-ai-native-bi
shinyaa31
0
130
セキュリティ専任チームなしで攻撃を先回りするSREが実践したSIEM on Amazon OpenSearch Service
cscengineer
PRO
1
730
2026 Apache Flink Community Checkpoint
rmetzger
0
210
[General] 개발자로 해외 기업에서 일하기
skydoves
1
310
新卒エンジニア、 介護士になる
ryu1013
1
690
Cloudflareの向き先
chimame
1
160
自治体AIコンタクトセンター、何をどこに任せる?~Amazon Connect Customer +Amazon Bedrockの実践知~ / 20261107 Satoshi Torano
shift_evolve
PRO
1
130
慶應義塾大学 AI実践05 論文執筆の仕組み化
keio_smilab
PRO
7
3.8k
The Author Pays
hiccupps
0
150
Featured
See All Featured
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
25
2.2k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
33
5.7k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
123
22k
Ecommerce SEO: The Keys for Success Now & Beyond - #SERPConf2024
aleyda
1
2.2k
<Decoding/> the Language of Devs - We Love SEO 2024
nikkihalliwell
1
380
The Hidden Cost of Media on the Web [PixelPalooza 2025]
tammyeverts
2
580
Bootstrapping a Software Product
garrettdimon
PRO
306
120k
Dominate Local Search Results - an insider guide to GBP, reviews, and Local SEO
greggifford
PRO
0
400
The innovator’s Mindset - Leading Through an Era of Exponential Change - McGill University 2025
jdejongh
PRO
1
400
The #1 spot is gone: here's how to win anyway
tamaranovitovic
4
1.2k
Faster Mobile Websites
deanohume
310
32k
Self-Hosted WebAssembly Runtime for Runtime-Neutral Checkpoint/Restore in Edge–Cloud Continuum
chikuwait
0
930
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