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 Foo | |
def foo | |
:foo | |
end | |
end | |
class Bar | |
include Foo | |
def bar | |
:bar | |
end | |
end | |
class Buz | |
extend Foo | |
def buz | |
:buz | |
end | |
end | |
# Bar Instance methods | |
p Bar.new.class.instance_methods.grep(/bar|foo/) # => [:bar, :foo] | |
# Buz Instance methods | |
p Buz.new.class.instance_methods.grep(/buz|foo/) # => [:buz] | |
# Buz Class methods | |
p Buz.new.class.methods.grep(/foo|buz/) # => [:foo] | |
# a list of modules and classes | |
p Bar.ancestors # => [Bar, Foo, Object, Kernel, BasicObject] | |
# singleton methods | |
p Bar.singleton_methods # => [] | |
# a list of modules and classes | |
p Buz.ancestors # => [Buz, Object, Kernel, BasicObject] | |
# singleton methods | |
p Buz.singleton_methods # => [:foo] |
つまりはこういうことです
ポイントは32行目と38行目
ancestorsメソッドでご先祖さんを取得しています。
includeしたbarクラスは、barとfooが取得できていますが、
extendしたbuzクラスは、fooが取得できていません。
extendはクラスに定義されるのではなく、
selfの特異メソッドとして定義されるのですねー
間違えてたら教えてw
( ̄(エ) ̄)彡☆
参考
instance method Module#ancestors
instance method Object#singleton_methods
instance method Enumerable#grep
instance method Module#instance_methods
0 件のコメント:
コメントを投稿