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
100
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
62
Writing config files in Ruby
arnvald
0
98
Speaking at RDRC
arnvald
0
90
Read more
arnvald
2
71
Your API is too slow!
arnvald
0
660
The simplest gem you'll ever use
arnvald
0
67
International to global
arnvald
0
61
Patterns, patterns everywhere
arnvald
0
72
Nomadic programmer - Baruco 2014 edition
arnvald
0
120
Other Decks in Technology
See All in Technology
FastMCPでSQLをチェックしてくれるMCPサーバーを自作してCursorから動かしてみた
nayuts
1
220
コードの考古学 〜労務システムから発掘した成長の糧〜
kenta_smarthr
1
1.2k
Data Hubグループ 紹介資料
sansan33
PRO
0
1.7k
從開發到架構設計的可觀測性實踐
philipz
0
120
Redmineの意外と知らない便利機能 (Redmine 6.0対応版)
vividtone
0
1.3k
Swiftは最高だよの話
yuukiw00w
2
290
2025advance01
minamizaki
0
130
GitHub Copilot Use Cases at ZOZO
horie1024
0
110
名刺メーカーDevグループ 紹介資料
sansan33
PRO
0
740
Java で学ぶ 代数的データ型
ysknsid25
1
710
会社員しながら本を書いてきた知見の共有
sat
PRO
3
690
TechBull Membersの開発進捗どうですか!?
rvirus0817
0
230
Featured
See All Featured
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
32
2.3k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
331
21k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
656
60k
A better future with KSS
kneath
239
17k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
29
9.5k
Building a Scalable Design System with Sketch
lauravandoore
462
33k
Build The Right Thing And Hit Your Dates
maggiecrowley
35
2.7k
Facilitating Awesome Meetings
lara
54
6.4k
Optimizing for Happiness
mojombo
378
70k
Mobile First: as difficult as doing things right
swwweet
223
9.6k
Agile that works and the tools we love
rasmusluckow
329
21k
How to Ace a Technical Interview
jacobian
276
23k
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