Created
December 23, 2010 08:00
-
-
Save igaiga/752718 to your computer and use it in GitHub Desktop.
alias_method_chain_test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
# $ rails runner self.rb | |
class C | |
def foo | |
'FOO' | |
end | |
end | |
class C | |
# 既存メソッドfooと一緒に呼ぶ新しいメソッド foo_with_XXXを定義 | |
def foo_with_bar | |
foo_without_bar + '_BAR' # foo_without_XXX で前に定義していたfoo が呼べる | |
end | |
alias_method_chain :foo, :bar # foo_with_XXX を :XXX という形式で書く | |
end | |
c = C.new | |
puts c.foo #=> "FOO_BAR" # 既存foo の代わりにfoo_with_bar が呼ばれる | |
puts c.foo_with_bar #=> "FOO_BAR" | |
puts c.foo_without_bar #=> "FOO" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment