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
180
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
100
Writing config files in Ruby
arnvald
0
170
Speaking at RDRC
arnvald
0
170
Read more
arnvald
2
130
Your API is too slow!
arnvald
0
750
The simplest gem you'll ever use
arnvald
0
140
International to global
arnvald
0
140
Patterns, patterns everywhere
arnvald
0
160
Nomadic programmer - Baruco 2014 edition
arnvald
0
150
Other Decks in Technology
See All in Technology
Multicaで30個のミニプロジェクトをAIエージェント運用して見えてきたこと
eiei114
1
620
アップデートで何が変わった?デモで学んで使いこなすIBM Bob2.0
muehara
0
220
AI時代におけるエンジニアの新たな役割──FDEとクオリアの探求/登壇資料(戸井田 裕貴)
hacobu
PRO
0
300
書籍セキュアAPIについて
riiimparm
0
160
ダッシュボード"開発"について 〜使われるダッシュボードのつくりかた〜
kimichan
0
190
ここは地獄!つらい朝会を体験することで、チームとしてのより良い振る舞いに気づくワークショップ / The stand-up meeting from hell in the game industry
scrummasudar
0
210
AI工学特論: MLOps・継続的評価
asei
4
500
VPCセキュリティ対応の最新事情
nagisa53
1
190
なぜ、あなたのAPIは使われないのか? AX時代の設計原則、ガードレール、運用体制
yokawasa
1
200
事業成長とAI活用を止めないデータ基盤アーキテクチャの設計思想
hiracky16
0
420
Type-safe IaC for Dart
coborinai
0
180
AI Coding Agent時代のcdk-nagガードレール 〜組織ルールを強制CIで守り抜く設計の挑戦〜
mhrtech
3
500
Featured
See All Featured
Automating Front-end Workflow
addyosmani
1370
210k
Discover your Explorer Soul
emna__ayadi
2
1.2k
What's in a price? How to price your products and services
michaelherold
247
13k
Optimizing for Happiness
mojombo
378
71k
Designing Powerful Visuals for Engaging Learning
tmiket
1
450
How to Build an AI Search Optimization Roadmap - Criteria and Steps to Take #SEOIRL
aleyda
1
2.1k
Navigating Algorithm Shifts & AI Overviews - #SMXNext
aleyda
1
1.5k
Making Projects Easy
brettharned
120
6.7k
Design of three-dimensional binary manipulators for pick-and-place task avoiding obstacles (IECON2024)
konakalab
0
490
Navigating Team Friction
lara
192
16k
The #1 spot is gone: here's how to win anyway
tamaranovitovic
3
1.1k
エンジニアに許された特別な時間の終わり
watany
108
250k
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