简体   繁体   English

instance&class 方法 include&extend 有什么区别(Ruby、Rails)

[英]What is the difference between instance&class method include&extend (Ruby, Rails)

What's the difference between class method and instance method.类方法和实例方法有什么区别。

I need to use some functions in a helper "RemoteFocusHelper" (under app/helpers/)我需要在助手“RemoteFocusHelper”中使用一些功能(在 app/helpers/ 下)

Then include the helper "RemoteFocusHelper" in the Worker module然后在 Worker 模块中包含 helper "RemoteFocusHelper"

But when I tried to call 'check_environment' (defined in RemoteFocusHelper ),但是当我尝试调用“check_environment”(在RemoteFocusHelper 中定义)时,

It raised ""no method error"".它引发了“无方法错误”。

Instead of using "include", I used the "extend" and works.我没有使用“包含”,而是使用了“扩展”并起作用。

I wonder know if it is correct that we can only use a class method when in a class method.我想知道我们只能在类方法中使用类方法是否正确。

Is it possible to call a instance method in a class method ?是否可以在类方法中调用实例方法?

By the way,how does the rake resque:work QUEUE='*' know where to search the RemoteFocusHelper I didn't give it the file path.Is the rake command will trace all files under the Rails app?顺便说一下, rake resque:work QUEUE='*'怎么知道在哪里搜索RemoteFocusHelper我没有给它文件路径。rake 命令会跟踪 Rails 应用程序下的所有文件吗?

automation_worker.rb


    class AutomationWorker
      @queue = :automation

      def self.perform(task=false)
        include RemoteFocusHelper
        if task
          ap task
          binding.pry
          check_environment
        else
          ap "there is no task to do"      
        end
      end
    end

The difference is the context where you're executing.不同之处在于您执行的上下文。 Pretty much every tutorial will have include or extend under the class :几乎每个教程都会在classincludeextend

class Foo
  include Thingy
end

class Bar
  extend Thingy
end

This will get executed at the time the class is defined: self is Foo (or Bar ) (of type Class ).这将在定义类时执行: selfFoo (或Bar )(类型Class )。 extend will thus dump the module contents into self - which creates class methods.因此, extend会将模块内容转储到self - 这会创建类方法。

When you do it inside a method definition, self is the instance object (of type Foo or Bar ).当您在方法定义中执行此操作时, self是实例对象(类型FooBar )。 Thus the place where the module gets dumped into changes.因此,模块被转储到变化的地方。 Now if you extend (the module contents), it dumps them into what is now self - resulting in instance methods.现在,如果您extend (模块内容),它会将它们转储到现在是self - 从而产生实例方法。

EDIT: It is also worth noting that because extend works on any instance object, it is defined on Object .编辑:还值得注意的是,因为extend适用于任何实例对象,所以它是在Object上定义的。 However, since only modules and classes are supposed to be able to include stuff, include is an instance method of Module class (and, by inheritance, Class as well).然而,因为只有模块和类才能包含东西,所以includeModule类的一个实例方法(并且,通过继承, Class也是如此)。 As a consequence of this, if you try putting include inside a definition of an instance method, it will fail hard, since most things (including your AutomationWorker ) are not descended from Module , and thus do not have access to the include method.因此,如果您尝试将include放入实例方法的定义中,它将很难失败,因为大多数事物(包括您的AutomationWorker )都不是从Module继承的,因此无法访问include方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM