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
アプリにAIを正しく組み込むための アーキテクチャ── 国産LLMの現実と実践
kohju
0
230
障害対応訓練、その前に
coconala_engineer
0
200
事業の財務責任に向き合うリクルートデータプラットフォームのFinOps
recruitengineers
PRO
2
220
Oracle Database@AWS:サービス概要のご紹介
oracle4engineer
PRO
1
410
M&Aで拡大し続けるGENDAのデータ活用を促すためのDatabricks権限管理 / AEON TECH HUB #22
genda
0
240
20251203_AIxIoTビジネス共創ラボ_第4回勉強会_BP山崎.pdf
iotcomjpadmin
0
140
Microsoft Agent Frameworkの可観測性
tomokusaba
1
110
Strands AgentsとNova 2 SonicでS2Sを実践してみた
yama3133
1
1.9k
_第4回__AIxIoTビジネス共創ラボ紹介資料_20251203.pdf
iotcomjpadmin
0
140
ハッカソンから社内プロダクトへ AIエージェント「ko☆shi」開発で学んだ4つの重要要素
sonoda_mj
6
1.7k
通勤手当申請チェックエージェント開発のリアル
whisaiyo
3
470
マイクロサービスへの5年間 ぶっちゃけ何をしてどうなったか
joker1007
21
8.2k
Featured
See All Featured
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
9
1.1k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
508
140k
Facilitating Awesome Meetings
lara
57
6.7k
Thoughts on Productivity
jonyablonski
73
5k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
37
6.2k
Odyssey Design
rkendrick25
PRO
0
440
Done Done
chrislema
186
16k
SEOcharity - Dark patterns in SEO and UX: How to avoid them and build a more ethical web
sarafernandez
0
89
Exploring anti-patterns in Rails
aemeredith
2
210
Getting science done with accelerated Python computing platforms
jacobtomlinson
0
79
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
122
21k
Music & Morning Musume
bryan
46
7k
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