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
maimai77
June 21, 2016
Programming
0
62
Method Wrapper
Introduction to ruby method wrappers
maimai77
June 21, 2016
Tweet
Share
Other Decks in Programming
See All in Programming
Let's learn code review
riofujimon
2
580
見た目から始める生産性向上
ikumatadokoro
10
1.4k
AWS CDKコントリビュートTIPS / aws-cdk-contribution-tips
gotok365
4
380
CA.swift19 恋するAIアプリ開発の裏側
oskmr
0
380
2 週間で Twitter Bot を作ってみた
contour_gara
0
770
検証も兼ねて個人開発でHonoとかと向き合った話
hanetsuki
1
1.3k
Domain-Driven Transformation
hschwentner
2
1.5k
Exploring the Implementation of “t.Run”, “t.Parallel”, and “t.Cleanup”
akarin
1
110
Azure OpenAI Serviceのプロンプトエンジニアリング入門
tomokusaba
3
870
使ってみよう Azure AI Document Intelligence
kosmosebi
2
360
Polars入門
daikikatsuragawa
1
170
デフォルトにして至高、RubyMineの大好きな所
ruzia
0
810
Featured
See All Featured
A designer walks into a library…
pauljervisheath
201
23k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
123
39k
GitHub's CSS Performance
jonrohan
1025
450k
VelocityConf: Rendering Performance Case Studies
addyosmani
321
23k
Why Our Code Smells
bkeepers
PRO
331
56k
What the flash - Photography Introduction
edds
64
11k
Reflections from 52 weeks, 52 projects
jeffersonlam
345
19k
Designing the Hi-DPI Web
ddemaree
276
33k
Rebuilding a faster, lazier Slack
samanthasiow
74
8.2k
A Tale of Four Properties
chriscoyier
152
22k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
228
16k
Writing Fast Ruby
sferik
622
60k
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ΛࣺͯΑ͏