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
130
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
69
Writing config files in Ruby
arnvald
0
120
Speaking at RDRC
arnvald
0
120
Read more
arnvald
2
82
Your API is too slow!
arnvald
0
700
The simplest gem you'll ever use
arnvald
0
86
International to global
arnvald
0
79
Patterns, patterns everywhere
arnvald
0
97
Nomadic programmer - Baruco 2014 edition
arnvald
0
130
Other Decks in Technology
See All in Technology
バグと向き合い、仕組みで防ぐ
____rina____
0
270
CodexでもAgent Skillsを使いたい
gotalab555
9
4.6k
「データ無い! 腹立つ! 推論する!」から 「データ無い! 腹立つ! データを作る」へ チームでデータを作り、育てられるようにするまで / How can we create, use, and maintain data ourselves?
moznion
7
4.1k
探求の技術
azukiazusa1
7
1.8k
Flutter DevToolsで発見! 本番アプリのパフォーマンス問題と改善の実践
goto_tsl
1
530
コンピューティングリソース何を使えばいいの?
tomokusaba
1
160
Amazon ECS デプロイツール ecspresso の開発を支える「正しい抽象化」の探求 / YAPC::Fukuoka 2025
fujiwara3
12
2.3k
データとAIで未来を創るDatabricks - 君の可能性を加速させるプラットフォーム
taka_aki
0
110
【M3】攻めのセキュリティの実践!プロアクティブなセキュリティ対策の実践事例
axelmizu
0
130
[mercari GEARS 2025] Keynote
mercari
PRO
0
210
Flutterコントリビューションのススメ
d_r_1009
1
380
Javaコミュニティの歩き方 ~参加から貢献まで、すべて教えます~
tabatad
0
100
Featured
See All Featured
The World Runs on Bad Software
bkeepers
PRO
72
12k
Documentation Writing (for coders)
carmenintech
76
5.1k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
26
3.2k
Git: the NoSQL Database
bkeepers
PRO
432
66k
StorybookのUI Testing Handbookを読んだ
zakiyama
31
6.3k
Balancing Empowerment & Direction
lara
5
740
Automating Front-end Workflow
addyosmani
1371
200k
Thoughts on Productivity
jonyablonski
73
4.9k
GraphQLの誤解/rethinking-graphql
sonatard
73
11k
Reflections from 52 weeks, 52 projects
jeffersonlam
355
21k
Speed Design
sergeychernyshev
32
1.2k
The Power of CSS Pseudo Elements
geoffreycrofte
80
6.1k
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