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
190
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Coercion in Ruby
Grzegorz Witek
May 09, 2018
More Decks by Grzegorz Witek
See All by Grzegorz Witek
One Year with Hanami
arnvald
0
110
Writing config files in Ruby
arnvald
0
180
Speaking at RDRC
arnvald
0
180
Read more
arnvald
2
140
Your API is too slow!
arnvald
0
760
The simplest gem you'll ever use
arnvald
0
150
International to global
arnvald
0
150
Patterns, patterns everywhere
arnvald
0
170
Nomadic programmer - Baruco 2014 edition
arnvald
0
150
Other Decks in Technology
See All in Technology
Claude Codeで開発以外の業務も爆速化しよう!
minorun365
PRO
12
9.6k
PM領域でのAI Agentの活用
lycorptech_jp
PRO
0
240
KPIだけでは評価できないプロダクトが考えるべき Evalsという第二の評価系 / Beyond KPIs: Evals as a Second Evaluation Framework for Products #PdEConf
aki_iinuma
3
3.3k
JAWS-UG初心者支部#88わいわい初心塾(夏休みの宿題やったかGit編)
otsuki
0
110
Guerilla InnerSource in enterprises, during the AI hype
onenashev
PRO
0
140
V8コントリビュート超入門
riyaamemiya
0
130
プロダクトエンジニアに必要な「いい感じ」に作る能力 〜たくさん作れる時代に、どこまで作るかの決め方〜
jnishime_dresscode
2
1.1k
AI時代におけるプロダクト横断勉強会の設計
zozotech
PRO
0
130
生成AI時代の クレデンシャルとパーミッション設計
nrinetcom
PRO
3
1.5k
AndroidでHDRメディアを「壊さずに」扱う
chigichan24
0
350
『止めない』を設計する — 制約の中で、事業の根幹を支える判断
hiroyaterui
0
130
少人数データチームのDevin活用実践事例
runandy16
2
420
Featured
See All Featured
Building Flexible Design Systems
yeseniaperezcruz
330
41k
Java REST API Framework Comparison - PWX 2021
mraible
34
9.7k
Bioeconomy Workshop: Dr. Julius Ecuru, Opportunities for a Bioeconomy in West Africa
akademiya2063
PRO
1
330
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
PRO
201
75k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
232
55k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
128
56k
Collaborative Software Design: How to facilitate domain modelling decisions
baasie
1
310
The Illustrated Children's Guide to Kubernetes
chrisshort
51
53k
The Organizational Zoo: Understanding Human Behavior Agility Through Metaphoric Constructive Conversations (based on the works of Arthur Shelley, Ph.D)
kimpetersen
PRO
0
430
How to Grow Your eCommerce with AI & Automation
katarinadahlin
PRO
1
260
Between Models and Reality
mayunak
4
440
The browser strikes back
jonoalderson
0
1.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