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
74
Method Wrapper
Introduction to ruby method wrappers
maimai77
June 21, 2016
Tweet
Share
Other Decks in Programming
See All in Programming
AI 駆動開発におけるコミュニティと AWS CDK の価値
konokenj
5
290
Pythonに漸進的に型をつける
nealle
1
130
バッチ処理を「状態の記録」から「事実の記録」へ
panda728
PRO
0
190
登壇は dynamic! な営みである / speech is dynamic
da1chi
0
380
なんでRustの環境構築してないのにRust製のツールが動くの? / Why Do Rust-Based Tools Run Without a Rust Environment?
ssssota
14
47k
Building, Deploying, and Monitoring Ruby Web Applications with Falcon (Kaigi on Rails 2025)
ioquatix
4
2.6k
外接に惑わされない自システムの処理時間SLIをOpenTelemetryで実現した話
kotaro7750
0
110
オンデバイスAIとXcode
ryodeveloper
0
260
SwiftDataを使って10万件のデータを読み書きする
akidon0000
0
240
Cursorハンズオン実践!
eltociear
2
1.2k
Things You Thought You Didn’t Need To Care About That Have a Big Impact On Your Job
hollycummins
0
260
Software Architecture
hschwentner
6
2.3k
Featured
See All Featured
[RailsConf 2023] Rails as a piece of cake
palkan
57
5.9k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
140
34k
The Language of Interfaces
destraynor
162
25k
Documentation Writing (for coders)
carmenintech
75
5.1k
Writing Fast Ruby
sferik
630
62k
Code Reviewing Like a Champion
maltzj
526
40k
Optimising Largest Contentful Paint
csswizardry
37
3.5k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
34
2.3k
Building Better People: How to give real-time feedback that sticks.
wjessup
370
20k
Build your cross-platform service in a week with App Engine
jlugia
234
18k
The Cult of Friendly URLs
andyhume
79
6.6k
Optimizing for Happiness
mojombo
379
70k
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ΛࣺͯΑ͏