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
5つのアンチパターンから学ぶLT設計
narihara
1
110
Julia という言語について (FP in Julia « SIDE: F ») for 関数型まつり2025
antimon2
3
980
ASP.NETアプリケーションのモダナイズ インフラ編
tomokusaba
1
410
すべてのコンテキストを、 ユーザー価値に変える
applism118
2
680
なぜ「共通化」を考え、失敗を繰り返すのか
rinchoku
1
470
GraphRAGの仕組みまるわかり
tosuri13
7
480
Haskell でアルゴリズムを抽象化する / 関数型言語で競技プログラミング
naoya
17
4.9k
レガシーシステムの機能調査・開発におけるAI利活用
takuya_ohtonari
0
610
Blazing Fast UI Development with Compose Hot Reload (droidcon New York 2025)
zsmb
1
190
Benchmark
sysong
0
250
Java on Azure で LangGraph!
kohei3110
0
170
20250628_非エンジニアがバイブコーディングしてみた
ponponmikankan
0
340
Featured
See All Featured
Stop Working from a Prison Cell
hatefulcrawdad
270
20k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
657
60k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
34
3k
What's in a price? How to price your products and services
michaelherold
246
12k
Bash Introduction
62gerente
614
210k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
161
15k
[RailsConf 2023] Rails as a piece of cake
palkan
55
5.6k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
35
2.3k
Building Adaptive Systems
keathley
43
2.6k
Measuring & Analyzing Core Web Vitals
bluesmoon
7
490
Become a Pro
speakerdeck
PRO
28
5.4k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
29
9.5k
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ΛࣺͯΑ͏