Skip to content

Instantly share code, notes, and snippets.

@rinmu
Last active November 22, 2017 11:50
Show Gist options
  • Select an option

  • Save rinmu/828f0eb88d7882755f9d to your computer and use it in GitHub Desktop.

Select an option

Save rinmu/828f0eb88d7882755f9d to your computer and use it in GitHub Desktop.
クラスメソッドを alias_method_chain ref: https://qiita.com/rin_mu/items/42b6090bbc0ba3574297
class A
def foo
"foo"
end
end
class B < A
def foo_with_bar
"foo bar"
end
alias_method_chain :foo, :bar
end
a = A.new
p a.foo #=> "foo"
b = B.new
p b.foo #=> "foo"
p b.foo_without_bar #=> "foo bar"
class A
def self.foo
"foo"
end
end
class B < A
class << self
def foo_with_bar
"foo bar"
end
alias_method_chain :foo, :bar
end
end
p A.foo #=> "foo"
p B.foo #=> "foo"
p B.foo_without_bar #=> "foo bar"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment