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
91
Your API is too slow!
arnvald
0
710
The simplest gem you'll ever use
arnvald
0
94
International to global
arnvald
0
88
Patterns, patterns everywhere
arnvald
0
110
Nomadic programmer - Baruco 2014 edition
arnvald
0
130
Other Decks in Technology
See All in Technology
MySQLのSpatial(GIS)機能をもっと充実させたい ~ MyNA望年会2025LT
sakaik
0
120
TED_modeki_共創ラボ_20251203.pdf
iotcomjpadmin
0
150
Next.js 16の新機能 Cache Components について
sutetotanuki
0
190
業務の煩悩を祓うAI活用術108選 / AI 108 Usages
smartbank
9
12k
SQLだけでマイグレーションしたい!
makki_d
0
1.2k
AWSの新機能をフル活用した「re:Inventエージェント」開発秘話
minorun365
2
460
NIKKEI Tech Talk #41: セキュア・バイ・デザインからクラウド管理を考える
sekido
PRO
0
210
ハッカソンから社内プロダクトへ AIエージェント ko☆shi 開発で学んだ4つの重要要素
leveragestech
0
190
100以上の新規コネクタ提供を可能にしたアーキテクチャ
ooyukioo
0
260
通勤手当申請チェックエージェント開発のリアル
whisaiyo
3
470
Oracle Database@AWS:サービス概要のご紹介
oracle4engineer
PRO
1
410
Oracle Database@Azure:サービス概要のご紹介
oracle4engineer
PRO
2
200
Featured
See All Featured
Leveraging Curiosity to Care for An Aging Population
cassininazir
1
130
What does AI have to do with Human Rights?
axbom
PRO
0
1.9k
A Modern Web Designer's Workflow
chriscoyier
698
190k
Navigating Team Friction
lara
191
16k
Building Flexible Design Systems
yeseniaperezcruz
330
39k
WENDY [Excerpt]
tessaabrams
8
35k
sira's awesome portfolio website redesign presentation
elsirapls
0
89
Exploring anti-patterns in Rails
aemeredith
2
210
The Pragmatic Product Professional
lauravandoore
37
7.1k
Site-Speed That Sticks
csswizardry
13
1k
New Earth Scene 8
popppiees
0
1.2k
Designing for Performance
lara
610
69k
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