Upgrade to PRO for Only $50/Year—Limited-Time Offer! 🔥
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Coercion in Ruby
Search
Grzegorz Witek
May 09, 2018
Technology
1
140
Coercion in Ruby
Grzegorz Witek
May 09, 2018
Tweet
Share
More Decks by Grzegorz Witek
See All by Grzegorz Witek
One Year with Hanami
arnvald
0
73
Writing config files in Ruby
arnvald
0
120
Speaking at RDRC
arnvald
0
130
Read more
arnvald
2
89
Your API is too slow!
arnvald
0
710
The simplest gem you'll ever use
arnvald
0
93
International to global
arnvald
0
85
Patterns, patterns everywhere
arnvald
0
110
Nomadic programmer - Baruco 2014 edition
arnvald
0
130
Other Decks in Technology
See All in Technology
Debugging Edge AI on Zephyr and Lessons Learned
iotengineer22
0
220
Kiro Autonomous AgentとKiro Powers の紹介 / kiro-autonomous-agent-and-powers
tomoki10
0
530
プロンプトやエージェントを自動的に作る方法
shibuiwilliam
12
10k
モダンデータスタック (MDS) の話とデータ分析が起こすビジネス変革
sutotakeshi
0
500
Python 3.14 Overview
lycorptech_jp
PRO
1
120
ChatGPTで論⽂は読めるのか
spatial_ai_network
10
29k
Sansanが実践する Platform EngineeringとSREの協創
sansantech
PRO
2
910
年間40件以上の登壇を続けて見えた「本当の発信力」/ 20251213 Masaki Okuda
shift_evolve
PRO
1
140
Snowflakeでデータ基盤を もう一度作り直すなら / rebuilding-data-platform-with-snowflake
pei0804
6
1.6k
[CMU-DB-2025FALL] Apache Fluss - A Streaming Storage for Real-Time Lakehouse
jark
0
120
EM歴1年10ヶ月のぼくがぶち当たった苦悩とこれからへ向けて
maaaato
0
280
[デモです] NotebookLM で作ったスライドの例
kongmingstrap
0
160
Featured
See All Featured
Agile that works and the tools we love
rasmusluckow
331
21k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
231
54k
Side Projects
sachag
455
43k
Unsuck your backbone
ammeep
671
58k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
55
3.1k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
32
1.8k
The Hidden Cost of Media on the Web [PixelPalooza 2025]
tammyeverts
1
100
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
16
1.8k
The Art of Programming - Codeland 2020
erikaheidi
56
14k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
31
3k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
52
5.8k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
25
1.6k
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