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
1
72
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
56
Writing config files in Ruby
arnvald
0
67
Speaking at RDRC
arnvald
0
55
Read more
arnvald
2
45
Your API is too slow!
arnvald
0
610
The simplest gem you'll ever use
arnvald
0
42
International to global
arnvald
0
32
Patterns, patterns everywhere
arnvald
0
31
Nomadic programmer - Baruco 2014 edition
arnvald
0
110
Other Decks in Technology
See All in Technology
独自ツール開発でスタジオ撮影をDX!「VLS(Virtual LED Studio)」 / dx-studio-vls
cyberagentdevelopers
PRO
1
180
話題のGraphRAG、その可能性と課題を理解する
hide212131
4
1.5k
pandasはPolarsに性能面で追いつき追い越せるのか
vaaaaanquish
4
4.6k
生成AIと知識グラフの相互利用に基づく文書解析
koujikozaki
1
140
LeSSに潜む「隠れWF病」とその処方箋
lycorptech_jp
PRO
2
120
わたしとトラックポイント / TrackPoint tips
masahirokawahara
1
240
Datachain会社紹介資料(2024年11月) / Company Deck
datachain
3
16k
VPC間の接続方法を整理してみた #自治体クラウド勉強会
non97
1
850
グローバル展開を見据えたサービスにおける機械翻訳プラクティス / dp-ai-translating
cyberagentdevelopers
PRO
1
150
一休.comレストランにおけるRustの活用
kymmt90
3
580
バクラクにおける可観測性向上の取り組み
yuu26
3
420
いまさらのStorybook
ikumatadokoro
0
140
Featured
See All Featured
The Art of Programming - Codeland 2020
erikaheidi
51
13k
BBQ
matthewcrist
85
9.3k
KATA
mclloyd
29
13k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
107
49k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
32
1.8k
Side Projects
sachag
452
42k
Building Your Own Lightsaber
phodgson
102
6.1k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
26
1.4k
How GitHub (no longer) Works
holman
311
140k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
364
22k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
7
150
Facilitating Awesome Meetings
lara
49
6k
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