Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 实例与实例之间有什么区别;类方法包括&;扩展(Ruby、Rails)_Ruby On Rails_Ruby - Fatal编程技术网

Ruby on rails 实例与实例之间有什么区别;类方法包括&;扩展(Ruby、Rails)

Ruby on rails 实例与实例之间有什么区别;类方法包括&;扩展(Ruby、Rails),ruby-on-rails,ruby,Ruby On Rails,Ruby,类方法和实例方法有什么区别 我需要在助手“RemoteFocusHelper”(在app/helpers/下)中使用一些函数 然后在Worker模块中包含帮助器“RemoteFocusHelper” 但是当我试图调用“check_environment”(定义于RemoteFocusHelper)时 它引发了“无方法错误” 我没有使用“include”,而是使用了“extend”和works 我想知道我们只能在类方法中使用类方法是否正确 可以在类方法中调用实例方法吗 顺便问一下,rake res

类方法和实例方法有什么区别

我需要在助手“RemoteFocusHelper”(在app/helpers/下)中使用一些函数

然后在Worker模块中包含帮助器“RemoteFocusHelper”

但是当我试图调用“check_environment”(定义于RemoteFocusHelper)时

它引发了“无方法错误”

我没有使用“include”,而是使用了“extend”和works

我想知道我们只能在类方法中使用类方法是否正确

可以在类方法中调用实例方法吗

顺便问一下,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

区别在于您执行的上下文。几乎每个教程都会在
类下设置
包含
扩展

class Foo
  include Thingy
end

class Bar
  extend Thingy
end
这将在定义类时执行:
self
Foo
(或
Bar
)(类型为
class
extend
将因此将模块内容转储到
self
——这将创建类方法

在方法定义中执行此操作时,
self
是实例对象(类型为
Foo
Bar
)。因此,模块转储到更改中的位置。现在,如果您
扩展
(模块内容),它会将它们转储到现在的
self
——从而生成实例方法

编辑:还值得注意的是,由于
extend
适用于任何实例对象,因此它是在
object
上定义的。然而,由于只有模块和类应该能够包含东西,
include
Module
类的实例方法(通过继承,
class
)。因此,如果您尝试将
include
放入实例方法的定义中,它将很难失败,因为大多数东西(包括您的
AutomationWorker
)都不是从
模块
派生的,因此无法访问
include
方法