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
68
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
47
Writing config files in Ruby
arnvald
0
65
Speaking at RDRC
arnvald
0
50
Read more
arnvald
2
42
Your API is too slow!
arnvald
0
570
The simplest gem you'll ever use
arnvald
0
36
International to global
arnvald
0
31
Patterns, patterns everywhere
arnvald
0
29
Nomadic programmer - Baruco 2014 edition
arnvald
0
110
Other Decks in Technology
See All in Technology
What if...? 처음부터 다시 LLM 어플리케이션을 개발한다면
huffon
0
1k
サービス開発を前に進めるために 新米リードエンジニアが 取り組んだこと / Steps Taken by a Novice Lead Engineer to Advance Service Development
nologyance
0
180
[2024最新版]AWS Control Towerを使ったセキュアなマルチアカウント環境の作り方
hiashisan
0
270
データ分析を支える技術 生成AI再入門
ishikawa_satoru
0
380
Matterport を使ってクラスメソッド各拠点のバーチャルオフィスツアーを作成してみた
wakatsuki
0
160
AI研修【MIXI 24新卒技術研修】
mixi_engineers
PRO
0
130
テスト・設計研修【MIXI 24新卒技術研修】
mixi_engineers
PRO
0
170
ソフトウェアエンジニアリングの知見を活かして データ基盤をいい感じにする on Snowflake [MIERUNE BBQ #10]
mtpooh
2
150
AOAI Dev Day - Opening Session
yoshidashingo
2
470
運用改善、不都合な真実 / 20240722-ssmjp-kaizen
opelab
17
8.4k
Classmethod Odyssey 登壇資料
yamahiro
0
390
コンテナ・K8s研修 - 前半 コンテナ基礎・ハンズオン【MIXI 24新卒技術研修】
mixi_engineers
PRO
0
170
Featured
See All Featured
Design by the Numbers
sachag
277
18k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
248
20k
StorybookのUI Testing Handbookを読んだ
zakiyama
15
4.9k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
353
29k
Bash Introduction
62gerente
607
210k
The World Runs on Bad Software
bkeepers
PRO
63
11k
The MySQL Ecosystem @ GitHub 2015
samlambert
248
12k
A better future with KSS
kneath
231
17k
Large-scale JavaScript Application Architecture
addyosmani
506
110k
The Invisible Side of Design
smashingmag
294
50k
How GitHub Uses GitHub to Build GitHub
holman
471
290k
Building Flexible Design Systems
yeseniaperezcruz
323
37k
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