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
72
Method Wrapper
Introduction to ruby method wrappers
maimai77
June 21, 2016
Tweet
Share
Other Decks in Programming
See All in Programming
Navigating Dependency Injection with Metro
zacsweers
3
3.5k
意外と簡単!?フロントエンドでパスキー認証を実現する WebAuthn
teamlab
PRO
2
780
Swift Updates - Learn Languages 2025
koher
2
510
Navigation 2 を 3 に移行する(予定)ためにやったこと
yokomii
0
350
テストカバレッジ100%を10年続けて得られた学びと品質
mottyzzz
2
610
パッケージ設計の黒魔術/Kyoto.go#63
lufia
3
440
テストコードはもう書かない:JetBrains AI Assistantに委ねる非同期処理のテスト自動設計・生成
makun
0
540
Android 16 × Jetpack Composeで縦書きテキストエディタを作ろう / Vertical Text Editor with Compose on Android 16
cc4966
2
270
AI Coding Agentのセキュリティリスク:PRの自己承認とメルカリの対策
s3h
0
240
ProxyによるWindow間RPC機構の構築
syumai
3
1.2k
AI Agents: How Do They Work and How to Build Them @ Shift 2025
slobodan
0
100
@Environment(\.keyPath)那么好我不允许你们不知道! / atEnvironment keyPath is so good and you should know it!
lovee
0
130
Featured
See All Featured
Agile that works and the tools we love
rasmusluckow
330
21k
Why You Should Never Use an ORM
jnunemaker
PRO
59
9.5k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
656
61k
Building an army of robots
kneath
306
46k
The Cost Of JavaScript in 2023
addyosmani
53
8.9k
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
4k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
7
850
Keith and Marios Guide to Fast Websites
keithpitt
411
22k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
234
17k
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.4k
Building a Modern Day E-commerce SEO Strategy
aleyda
43
7.6k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
46
7.6k
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ΛࣺͯΑ͏