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
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
90
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
要件定義の精度を高めるための型と生成AIの活用 / Using Types and Generative AI to Improve the Accuracy of Requirements Definition
haru860
0
250
AIでAIをテストする - 音声AIエージェントの品質保証戦略
morix1500
1
160
アクセシビリティはすべての人のもの
tomokusaba
0
190
AI時代に越境し、 組織を変えるQAスキルの正体 / QA Skills for Transforming an Organization
mii3king
0
160
AndroidアプリとCopilot Studioの統合
nakasho
0
190
Forget technical debt
ufried
0
140
AWS Transform CustomでIaCコードを自由自在に変換しよう
duelist2020jp
0
220
Google Cloud Next '26 の裏でこっそりリリースされたCloud Number Registry & Cloud Hub コスト分析 を試してみた
hikaru1001
0
140
FessのAI検索モード:検索システムとLLMへの取り組み
marevol
0
150
音声言語モデル手法に関する発表の紹介
kzinmr
0
150
MySQL 9.7がやってきた ~これまでのあらすじと基本情報~ @ 日本MySQLユーザ会会2026年04月 / mysql97-yattekita
sakaik
0
150
M5Stack CoreS3とZephyr(RTOS)で Edge AIっぽいことしてみた
iotengineer22
0
400
Featured
See All Featured
Agile that works and the tools we love
rasmusluckow
331
21k
End of SEO as We Know It (SMX Advanced Version)
ipullrank
3
4.1k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
21
1.5k
AI Search: Implications for SEO and How to Move Forward - #ShenzhenSEOConference
aleyda
1
1.2k
Navigating the Design Leadership Dip - Product Design Week Design Leaders+ Conference 2024
apolaine
0
290
The B2B funnel & how to create a winning content strategy
katarinadahlin
PRO
1
340
Code Reviewing Like a Champion
maltzj
528
40k
Max Prin - Stacking Signals: How International SEO Comes Together (And Falls Apart)
techseoconnect
PRO
0
150
A designer walks into a library…
pauljervisheath
211
24k
How to build an LLM SEO readiness audit: a practical framework
nmsamuel
1
730
Ethics towards AI in product and experience design
skipperchong
2
260
The Impact of AI in SEO - AI Overviews June 2024 Edition
aleyda
5
820
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