Skip to content

Instantly share code, notes, and snippets.

@tagomoris
Created June 30, 2016 07:41
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 tagomoris/a45c64a172d5a5f2f7b7798dd0ab1151 to your computer and use it in GitHub Desktop.
Save tagomoris/a45c64a172d5a5f2f7b7798dd0ab1151 to your computer and use it in GitHub Desktop.
class A
def foo
"a"
end
end
class B < A
def foo
v = super
"b#{v}"
end
end
module M
def foo
v = super
v.upcase
end
end
b1 = B.new.extend(M)
p b1.singleton_class.ancestors
p b1.foo
b2 = B.new
b2.singleton_class.include(M)
p b2.singleton_class.ancestors
p b2.foo
b3 = B.new
b3.singleton_class.prepend(M)
p b3.singleton_class.ancestors
p b3.foo
ruby hoge.rb
[#<Class:#<B:0x007f98b9962c70>>, M, B, A, Object, Kernel, BasicObject]
"BA"
[#<Class:#<B:0x007f98b99626a8>>, M, B, A, Object, Kernel, BasicObject]
"BA"
[M, #<Class:#<B:0x007f98b9962180>>, B, A, Object, Kernel, BasicObject]
"BA"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment