开发者

determine the context on which a method runs

class Class
  def mixin_ancestors(include_ancestors=true)
    ancestors.take_while {|a| include_ancestors || a != superclass }.
    select {|ancestor| ancestor.instance_of?(Module) }
  end
end

class MyTestClass

end

Took the above code from How do you list included Modules in a Ruby Class?

I have following questions.

1) By saying 'def mixin_ancestors' as in the above code we are defining an instance method. But doing 'a = MyTestClass.new ; a.mixin_ancestors' says undefined method mixin_ancestors.

2) So I did ‘MyTestClass. mixin_ancestors’. It gave me a list .

开发者_开发百科

3) I think ‘ancestors’ is a method. In which context does the ‘ancestors’ method runs. To find that I did ‘method(ancestors).owner’ but got error- method: [MyTestClass, Object, Kernel, BasicObject] is not a symbol (TypeError). Got this trick from my own previous question Determine the class to which a method belongs in rails

4) Like third point on which context does superclass method in the above code runs.

Thanks for the helps


1 & 2) We are defining an instance method of Class which means it will be a class method for other objects.

3) It is run within Class object, so it is a method of Class or one of it's ancestors (it's actually in BasicObject)

4) It is run in context of Class object (or of object extending it)


In ruby, Class is a class for class object. In your example, MyTestClass is an instance of class Class. So, mixin_ancestor is an instance method for these objects.

In 3), you should use method(:ancestors), but you simply call it - and it returned an array - as you can see in output - [MyTestClass, Object, Kernel, BasicObject]

4) self here is an instance of Class

I strongly recommend you to buy http://www.amazon.com/Metaprogramming-Ruby-Program-Like-Pros/dp/1934356476 - this book explains ruby in excellent way!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜