Skip to content

Instantly share code, notes, and snippets.

@igaiga
Created December 23, 2010 08:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save igaiga/752718 to your computer and use it in GitHub Desktop.
Save igaiga/752718 to your computer and use it in GitHub Desktop.
alias_method_chain_test
# -*- 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