Lock in $30 Savings on PRO—Offer Ends Soon! ⏳
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
90
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
年間40件以上の登壇を続けて見えた「本当の発信力」/ 20251213 Masaki Okuda
shift_evolve
PRO
1
140
ActiveJobUpdates
igaiga
1
240
Reinforcement Fine-tuning 基礎〜実践まで
ch6noota
0
200
Identity Management for Agentic AI 解説
fujie
0
170
AWS CLIの新しい認証情報設定方法aws loginコマンドの実態
wkm2
7
770
生成AI時代におけるグローバル戦略思考
taka_aki
0
210
re:Invent2025 3つの Frontier Agents を紹介 / introducing-3-frontier-agents
tomoki10
0
300
re:Invent 2025 ~何をする者であり、どこへいくのか~
tetutetu214
0
230
MLflowダイエット大作戦
lycorptech_jp
PRO
1
140
シニアソフトウェアエンジニアになるためには
kworkdev
PRO
3
200
Amazon Connect アップデート! AIエージェントにMCPツールを設定してみた!
ysuzuki
0
110
「図面」から「法則」へ 〜メタ視点で読み解く現代のソフトウェアアーキテクチャ〜
scova0731
0
400
Featured
See All Featured
A Modern Web Designer's Workflow
chriscoyier
698
190k
Neural Spatial Audio Processing for Sound Field Analysis and Control
skoyamalab
0
120
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
659
61k
The Pragmatic Product Professional
lauravandoore
37
7.1k
Code Reviewing Like a Champion
maltzj
527
40k
The browser strikes back
jonoalderson
0
63
Game over? The fight for quality and originality in the time of robots
wayneb77
1
60
Tips & Tricks on How to Get Your First Job In Tech
honzajavorek
0
390
Groundhog Day: Seeking Process in Gaming for Health
codingconduct
0
61
What Being in a Rock Band Can Teach Us About Real World SEO
427marketing
0
140
Understanding Cognitive Biases in Performance Measurement
bluesmoon
32
2.8k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
27k
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