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
84
Writing config files in Ruby
arnvald
0
150
Speaking at RDRC
arnvald
0
160
Read more
arnvald
2
100
Your API is too slow!
arnvald
0
730
The simplest gem you'll ever use
arnvald
0
110
International to global
arnvald
0
120
Patterns, patterns everywhere
arnvald
0
140
Nomadic programmer - Baruco 2014 edition
arnvald
0
130
Other Decks in Technology
See All in Technology
互換性のある(らしい)DBへの移行など考えるにあたってたいへんざっくり
sejima
PRO
0
550
Databricks Appsで実現する社内向けAIアプリ開発の効率化
r_miura
0
320
Hello UUID
mimifuwacc
0
110
Oracle Cloud Infrastructure:2026年3月度サービス・アップデート
oracle4engineer
PRO
0
400
Claude Teamプランの選定と、できること/できないこと
rfdnxbro
1
1k
40代からのアウトプット ― 経験は価値ある学びに変わる / 20260404 Naoki Takahashi
shift_evolve
PRO
5
850
Databricks Lakebaseを用いたAIエージェント連携
daiki_akimoto_nttd
0
160
組織的なAI活用を阻む 最大のハードルは コンテキストデザインだった
ixbox
1
780
建設的な現実逃避のしかた / How to practice constructive escapism
pauli
4
260
サイボウズフロントエンドの活動から考える探究と発信
mugi_uno
0
110
JSTQB Expert Levelシラバス「テストマネジメント」日本語版のご紹介
ymty
0
130
スケーリングを封じられたEC2を救いたい
senseofunity129
0
140
Featured
See All Featured
Future Trends and Review - Lecture 12 - Web Technologies (1019888BNR)
signer
PRO
0
3.4k
Principles of Awesome APIs and How to Build Them.
keavy
128
17k
For a Future-Friendly Web
brad_frost
183
10k
First, design no harm
axbom
PRO
2
1.2k
From Legacy to Launchpad: Building Startup-Ready Communities
dugsong
0
190
Evolving SEO for Evolving Search Engines
ryanjones
0
170
Game over? The fight for quality and originality in the time of robots
wayneb77
1
160
Navigating the Design Leadership Dip - Product Design Week Design Leaders+ Conference 2024
apolaine
0
260
Thoughts on Productivity
jonyablonski
76
5.1k
Large-scale JavaScript Application Architecture
addyosmani
515
110k
Navigating the moral maze — ethical principles for Al-driven product design
skipperchong
2
320
The innovator’s Mindset - Leading Through an Era of Exponential Change - McGill University 2025
jdejongh
PRO
1
140
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