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
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
120
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
770
The simplest gem you'll ever use
arnvald
0
160
International to global
arnvald
0
160
Patterns, patterns everywhere
arnvald
0
170
Nomadic programmer - Baruco 2014 edition
arnvald
0
150
Other Decks in Technology
See All in Technology
reInvent2026stby LT00 "Change your life!"
shinichirokawano
1
310
日本AIベンチャーレポート2026
kentarofujii
0
240
Kiro Crewで「みのるん式」仕事術やってみた
minorun365
PRO
3
1.1k
Claude Codeから見る、エンジニアの世界の変容とその先
oikon48
6
4.3k
CSS2026:OWS研究会・招待講演「世界観を、実装する」
atsush1_sada
2
340
データマネジメント × 法務で作りあげる、 freeeの新しいデータガバナンスへの軌跡
freee
PRO
1
430
DevelopersIO2026 -バックアップは“消されないこと”が9割-
yoshida0129
0
410
AIをリアル世界に解き放つ 〜M5Stack×AIであなたのアイディアを手の平に〜
hamadakoji
1
550
【データ横丁主催】AI エージェント時代のTranslytical Data Platform
shisyu_gaku
0
300
これだけは抑えておきたい!後悔しない re:Invent の過ごし方 / AWS re:Invent 2026 prestudy
kinunori
2
390
AIに任せられるFlutterコードベースを作る 〜仕様をコードで表し、ハーネスで守る〜
myohei
1
160
シンプルな設定ファイルで実現する AWS IAM Identity Center の ユーザー管理Terraformフレームワークを公開しました / Simplifying AWS IAM Identity Center User Management: An Open-Source Terraform Framework
yamaguchitk333
0
320
Featured
See All Featured
Become a Pro
speakerdeck
PRO
31
6.3k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
128
56k
Impact Scores and Hybrid Strategies: The future of link building
tamaranovitovic
0
470
Technical Leadership for Architectural Decision Making
baasie
3
610
Future Trends and Review - Lecture 12 - Web Technologies (1019888BNR)
signer
PRO
0
3.8k
Data-driven link building: lessons from a $708K investment (BrightonSEO talk)
szymonslowik
1
1.4k
A better future with KSS
kneath
240
18k
Navigating Weather and Climate Data
rabernat
0
560
Paper Plane (Part 1)
katiecoart
PRO
2
12k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
PRO
203
77k
Applied NLP in the Age of Generative AI
inesmontani
PRO
4
2.5k
BBQ
matthewcrist
90
10k
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