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
Method Wrapper
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
maimai77
June 21, 2016
Programming
0
74
Method Wrapper
Introduction to ruby method wrappers
maimai77
June 21, 2016
Tweet
Share
Other Decks in Programming
See All in Programming
責任感のあるCloudWatchアラームを設計しよう
akihisaikeda
3
160
Lambda のコードストレージ容量に気をつけましょう
tattwan718
0
100
0→1 フロントエンド開発 Tips🚀 #レバテックMeetup
bengo4com
0
540
高速開発のためのコード整理術
sutetotanuki
1
380
CSC307 Lecture 07
javiergs
PRO
0
550
Kotlin Multiplatform Meetup - Compose Multiplatform 외부 의존성 아키텍처 설계부터 운영까지
wisemuji
0
190
AIエージェントの設計で注意するべきポイント6選
har1101
7
3.4k
AI前提で考えるiOSアプリのモダナイズ設計
yuukiw00w
0
220
AI Agent の開発と運用を支える Durable Execution #AgentsInProd
izumin5210
7
2.3k
コントリビューターによるDenoのすゝめ / Deno Recommendations by a Contributor
petamoriken
0
200
Data-Centric Kaggle
isax1015
2
760
疑似コードによるプロンプト記述、どのくらい正確に実行される?
kokuyouwind
0
380
Featured
See All Featured
Faster Mobile Websites
deanohume
310
31k
Building an army of robots
kneath
306
46k
Ten Tips & Tricks for a 🌱 transition
stuffmc
0
64
ReactJS: Keep Simple. Everything can be a component!
pedronauck
666
130k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
52
5.8k
Documentation Writing (for coders)
carmenintech
77
5.2k
Abbi's Birthday
coloredviolet
1
4.7k
Visual Storytelling: How to be a Superhuman Communicator
reverentgeek
2
430
Navigating the moral maze — ethical principles for Al-driven product design
skipperchong
2
240
Between Models and Reality
mayunak
1
180
Rails Girls Zürich Keynote
gr2m
96
14k
Mind Mapping
helmedeiros
PRO
0
75
Transcript
Method Wrapper @maimai77 2016/06/08
What is Method Wrapper? 4 ϥΠϒϥϦʹฤूͰ͖ͳ͍ϝιου͕͋Δ 4 ͦͷϝιουͷपғʹػೳΛՃͯ͠ɺطʹଘࡏ͢Δݺग़ ݩʹมߋΛՃ͑Δ͜ͱͳࣗ͘ಈతʹ৽ػೳΛ͑ΔΑ͏ʹ ͍ͨ͠
4 ΠϯλʔϑΣʔεΛมߋͤͣʹɺػೳΛՃ͢Δख๏
Method Wrappers 4 Around Alias 4 Module#alias_method 4 Prepend Wrapper
4 Module#prepend ( ruby >= 2.0 ) 4 Refinements Wrapper 4 Module#refine ( ruby >= 2.0 but darkpowered)
Module#alias_method
Module#alias_method class MyClass def my_method 'my_method' end alias_method :m, :my_method
end obj = MyClass.new obj.my_method #=> "my_method" obj.m #=> "my_method"
Module#alias_method ྫ: String#rjust '123'.rjust(5) #=> " 123" '123'.rjust(5, '0') #=>
"00123" શͯͷݺͼग़͠ݩͰୈ2ҾͷσϑΥϧτΛ'0'ʹ͍ͨ͠
Module#alias_method class String def rjust_with_zero(integer, padstr=' ') padstr = '0'
if padstr == ' ' rjust_without_zero(integer, padstr) end alias_method :rjust_without_zero, :rjust alias_method :rjust, :rjust_with_zero end '123'.rjust(5) #=> "00123"
Module#alias_method alias_method :rjust_without_zero, :rjust alias_method :rjust, :rjust_with_zero # Ruby on
Rails alias_method_chain :rjust, :zero
But, on Rails5... def alias_method_chain(target, feature) ActiveSupport::Deprecation.warn( "alias_method_chain is deprecated.\
Please, use Module#prepend instead.\ From module, you can access the originalmethod using super." ) ... end
Module#prepend
Module#prepend ܧঝνΣʔϯͰincluderͷલʹૠೖ͞ΕΔ (લఏࣝ: ϝιου୳ࡧɺܧঝνΣʔϯ) module M end String.include M String.ancestors
#=> [String, M, Comparable, Object, Kernel, BasicObject] String.prepend M String.ancestors #=> [M, String, Comparable, Object, Kernel, BasicObject]
Module#prepend module RjustWithZero def rjust(integer, padstr=' ') padstr = '0'
if padstr == ' ' super end end String.prepend RjustWithZero '123'.rjust(5) #=> "00123"
Good and Bad Parts of Module#alias_method good - ϝιουͷఆ͕ٛຊདྷͷϝιουͱಉ͡ॴʹͳΔͷͰΘ͔Γ͍͢ʢʁʣ -
Ruby1.9Ҏલͷݪ࢝࣌ʹ͓͍ͯ΄΅།Ұͷखஈ bad - هड़͕ࡶ - _with_ϝιουͷఆٛͱalias_methodݺͼग़͠ͲΕ͚ͩΕ͍ͯͯྑ͍ʢϧʔϧ͕ඞཁʣ - ඞવతʹ3୯ޠҎ্ͷϝιου໊Λఆٛ͢Δ͜ͱʹͳΔͷͰμα͍ - ϝιουͷఆ͕ٛຊདྷͷϝιουͱಉ͡ॴʹͳΔͷͰ֦ுରΛԚછ͢Δ - ಉ͡alias_methodͷϖΞΛ2ճ࣮ߦͯ͠͠·͏ͱϝιου࣮ߦ࣌ʹࣗݾࢀরϧʔϓͰࢮ͵
Good and Bad Parts of Module#prepend good - هड़͕؆ܿ -
ϝιουͷఆ͕֦ٛுmoduleͰด͍ͯ͡Δ bad - Rubyͷཧղ͕ઙ͍ͱΘ͔Γʹ͍͘ - Method WrapperҎ֎ͷ༻్Ͱ͏ͱةݥ
Conclusion
Conclusion 4 ϝιου୳ࡧɺܧঝνΣʔϯΛཧղ͠Α͏ ʢRubyΛ͏্Ͱ࠷ݶʣ 4 Module#prependʹ׳ΕΑ͏ 4 Ruby1.9ΛࣺͯΑ͏