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
180
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
110
Writing config files in Ruby
arnvald
0
170
Speaking at RDRC
arnvald
0
170
Read more
arnvald
2
140
Your API is too slow!
arnvald
0
760
The simplest gem you'll ever use
arnvald
0
140
International to global
arnvald
0
140
Patterns, patterns everywhere
arnvald
0
160
Nomadic programmer - Baruco 2014 edition
arnvald
0
150
Other Decks in Technology
See All in Technology
属人化を叩き割れ!「制作加速」と「安定化」の矛盾を打破する:8年目プロダクトが辿り着いた「ミスが起こり得ない」アセット制作フローの全貌
gree_tech
PRO
0
120
Adding Right-to-Left support to your web application with CSS logical properties — Lessons from Redmine
vividtone
0
160
AI・HPC開発を支えるGPU環境の新しい選択肢 液冷GPUシステム「AquSys」の取り組み
gpuunite_official
0
160
Eight Engineering Unit 紹介資料
sansan33
PRO
3
8.2k
Sets in Go
ramalho
1
1.1k
AI時代のアウトプット――変わったこと、変わらないこと / Devsumi 2026 Kansai #devsumi
jnchito
0
330
Oracle MCP Servers Explained
thatjeffsmith
0
290
自宅NWにISR4331を導入してみた話
okaits
0
120
DatadogのBits Chatが開発組織にもたらしたもの / What Bits Chat Has Brought Us
sms_tech
1
290
Head First モブプログラミング / Head First Mobprogramming
takaking22
10
12k
三人寄ればチューリング完全
puhitaku
4
1.2k
医療の現場を変革に挑戦した半年間の軌跡 - PythonとAIで現場を変える / From Code to Care
soudai
PRO
0
350
Featured
See All Featured
Building Better People: How to give real-time feedback that sticks.
wjessup
370
20k
Abbi's Birthday
coloredviolet
3
9.4k
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
4.5k
Building AI with AI
inesmontani
PRO
1
1.1k
Making the Leap to Tech Lead
cromwellryan
135
10k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
232
55k
The Curse of the Amulet
leimatthew05
2
14k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
230
23k
Measuring & Analyzing Core Web Vitals
bluesmoon
9
970
Practical Orchestrator
shlominoach
191
12k
How to Talk to Developers About Accessibility
jct
2
520
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.8k
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