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
150
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
77
Writing config files in Ruby
arnvald
0
130
Speaking at RDRC
arnvald
0
140
Read more
arnvald
2
95
Your API is too slow!
arnvald
0
710
The simplest gem you'll ever use
arnvald
0
99
International to global
arnvald
0
100
Patterns, patterns everywhere
arnvald
0
130
Nomadic programmer - Baruco 2014 edition
arnvald
0
130
Other Decks in Technology
See All in Technology
日本語テキストと音楽の対照学習の技術とその応用
lycorptech_jp
PRO
1
380
学生・新卒・ジュニアから目指すSRE
hiroyaonoe
1
250
Claude Codeベストプラクティスまとめ
minorun365
54
31k
Vitest Highlights in Angular
rainerhahnekamp
0
120
MySQLのJSON機能の活用術
ikomachi226
0
120
しろおびセキュリティへ ようこそ
log0417
0
210
Amazon Bedrock AgentCore 認証・認可入門
hironobuiga
2
450
今日から始めるAmazon Bedrock AgentCore
har1101
4
250
Introduction to Sansan, inc / Sansan Global Development Center, Inc.
sansan33
PRO
0
2.9k
コスト削減から「セキュリティと利便性」を担うプラットフォームへ
sansantech
PRO
1
280
いよいよ仕事を奪われそうな波が来たぜ
kazzpapa3
3
310
分析画面のクリック操作をそのままコード化 ! エンジニアとビジネスユーザーが共存するAI-ReadyなBI基盤
ikumi
0
110
Featured
See All Featured
Sam Torres - BigQuery for SEOs
techseoconnect
PRO
0
170
Ruling the World: When Life Gets Gamed
codingconduct
0
130
Avoiding the “Bad Training, Faster” Trap in the Age of AI
tmiket
0
66
Designing Powerful Visuals for Engaging Learning
tmiket
0
210
世界の人気アプリ100個を分析して見えたペイウォール設計の心得
akihiro_kokubo
PRO
66
36k
The Limits of Empathy - UXLibs8
cassininazir
1
210
16th Malabo Montpellier Forum Presentation
akademiya2063
PRO
0
44
Exploring anti-patterns in Rails
aemeredith
2
230
The untapped power of vector embeddings
frankvandijk
1
1.6k
Dominate Local Search Results - an insider guide to GBP, reviews, and Local SEO
greggifford
PRO
0
51
Designing Dashboards & Data Visualisations in Web Apps
destraynor
231
54k
Lessons Learnt from Crawling 1000+ Websites
charlesmeaden
PRO
1
1.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