In Ruby or Rails, why is “include” sometimes inside the class and sometimes outside the class?

前端 未结 1 1227
小鲜肉
小鲜肉 2021-01-04 10:23

I thought

class ApplicationController < ActionController::Base
  include Foo

is to add a \"mixin\" -- so that all methods in the Foo mo

1条回答
  •  执笔经年
    2021-01-04 10:54

    Yes, include Foo inside a class adds Foo to that class's ancestors and thus makes all of Foo's instance methods available to instances of those class.

    Outside of any class definition include Foo will add Foo to the ancestors of Object. I.e. it is the same as if you did include Foo inside the definition of the Object class. The use doing this is that all of Foo's instance methods are now available everywhere.

    0 讨论(0)
提交回复
热议问题