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
64
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
45
Writing config files in Ruby
arnvald
0
61
Speaking at RDRC
arnvald
0
44
Read more
arnvald
2
40
Your API is too slow!
arnvald
0
540
The simplest gem you'll ever use
arnvald
0
29
International to global
arnvald
0
31
Patterns, patterns everywhere
arnvald
0
28
Nomadic programmer - Baruco 2014 edition
arnvald
0
110
Other Decks in Technology
See All in Technology
IPUT App Dev. Co. -Overview 2024/4
iputapp
0
130
生成AIの変革の時代に、直近1年で直面した課題とその解決策
ktc_wada
0
670
LayerXにおけるLLMプロダクト開発の今までとこれから
layerx
PRO
4
720
M&A戦略を支えるデータマネジメント (MIDAS Tech Study #16 GENDA Komiyama)
kommy339
1
110
障害対応をちょっとずつよくしていくための 演習の作りかた
heleeen
1
1.7k
実例で紹介するRAG導入時の知見と精度向上の勘所
yamahiro
5
1.6k
コードファーストの考え方。 Amplify Gen2から学ぶAWS次世代のWeb開発体験
yoshiitaka
2
370
20分で完全に理解するGrafanaダッシュボード
hamadakoji
5
890
How to do well in consulting–Balkan Ruby 2024
irinanazarova
0
140
家族アルバム みてねにおけるGrafana活用術 / Grafana Meetup Japan Vol.1 LT
isaoshimizu
1
1k
開発パフォーマンスを最大化するための開発体制
ham0215
7
1.1k
コードや知識を組み込む / Incorporate Code and knowledge
ks91
PRO
0
150
Featured
See All Featured
Building Adaptive Systems
keathley
32
1.9k
GraphQLとの向き合い方2022年版
quramy
33
12k
Producing Creativity
orderedlist
PRO
338
39k
Build The Right Thing And Hit Your Dates
maggiecrowley
25
2k
Git: the NoSQL Database
bkeepers
PRO
423
63k
[RailsConf 2023] Rails as a piece of cake
palkan
27
4k
Code Review Best Practice
trishagee
56
15k
We Have a Design System, Now What?
morganepeng
43
6.8k
Navigating Team Friction
lara
179
13k
Happy Clients
brianwarren
92
6.4k
What’s in a name? Adding method to the madness
productmarketing
PRO
17
2.7k
Scaling GitHub
holman
457
140k
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