Created
September 30, 2011 12:51
-
-
Save kokudori/1253650 to your computer and use it in GitHub Desktop.
特異クラスについて
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
module Piyo | |
def piyo_func | |
'piyo_func' | |
end | |
end | |
class Hoge | |
include Piyo | |
def initialize | |
class << self | |
def obj_sing_func | |
p "obj_sing_func" | |
end | |
end | |
end | |
def inst_func | |
p "inst_func" | |
end | |
end | |
class << Hoge | |
def class_sing_func | |
p "class_sing_func" | |
end | |
end | |
def Hoge.sing_func2 | |
p "sing_func2" | |
end | |
hoge = Hoge.new | |
def hoge.sing_func | |
p "sing_func" | |
end | |
p "-----object-----" | |
p " #{hoge}" | |
p " methods" | |
p " #{hoge.methods - Object.new.methods - hoge.methods(false)}" | |
p " singleton_methods" | |
p " #{hoge.methods false}" | |
p "-----class-----" | |
p " #{Hoge}" | |
p " methods" | |
p " #{Hoge.methods - Class.new.methods - Hoge.methods(false)}" | |
p " singleton_methods" | |
p " #{Hoge.methods false}" | |
p Hoge.ancestors | |
p "-----object's singleton class-----" | |
obj_sing = class << hoge | |
self | |
end | |
p " #{obj_sing}" | |
p " methods" | |
p " #{obj_sing.methods - Class.new.methods - obj_sing.methods(false)}" | |
p " singleton_methods" | |
p " #{obj_sing.methods false}" | |
p obj_sing.ancestors | |
p "-----class's singleton class-----" | |
class_sing = class << Hoge | |
self | |
end | |
p " #{class_sing}" | |
p " methods" | |
p " #{class_sing.methods - Class.new.methods - Module.methods - class_sing.methods(false)}" | |
p " singleton_methods" | |
p " #{class_sing.methods false}" | |
p class_sing.ancestors | |
csf = Hoge.method :class_sing_func | |
p "class_sing_func method's owner is #{csf.owner}" | |
p "class_sing.methods.grep /class_sing_func/ is #{class_sing.methods.grep /class_sing_func/}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
実行結果
"-----object-----"
" #Hoge:0x966c7dc"
" methods"
" [:inst_func, :piyo_func]"
" singleton_methods"
" [:obj_sing_func, :sing_func]"
"-----class-----"
" Hoge"
" methods"
" []"
" singleton_methods"
" [:class_sing_func, :sing_func2]"
[Hoge, Piyo, Object, Kernel, BasicObject]
"-----object's singleton class-----"
" #Class:#Hoge:0x966c7dc"
" methods"
" []"
" singleton_methods"
" [:class_sing_func, :sing_func2]"
[Hoge, Piyo, Object, Kernel, BasicObject]
"-----class's singleton class-----"
" #Class:Hoge"
" methods"
" []"
" singleton_methods"
" []"
[Class, Module, Object, Kernel, BasicObject]
"class_sing_func method's owner is #Class:Hoge"
"class_sing.methods.grep /class_sing_func/ is []"