简体   繁体   English

如何在Ruby中调用外部类的方法

[英]How to call methods of an external class in Ruby

I have a file under /lib with its own method. 我在/lib下有一个具有自己方法的文件。

# lib/file.rb
class File < ApplicationController
  def my_method
    ...
  end
end

However I can't reach the method through the console 但是我无法通过控制台找到方法

ruby-1.9.2-p290 :044 > File.my_method
NoMethodError: undefined method `my_method' for File:Class

Any idea how? 任何想法如何?

my_method is an instance method of the File class. my_methodFile类的实例方法。 It means that you can call it only on the instance of the File class. 这意味着您只能在File类的实例上调用它。

file = File.new
file.my_method

You can declare my_method as class method using def self.my_method syntax. 你可以声明my_method使用作为类方法def self.my_method语法。

class File < ApplicationController
  def self.my_method
    ...
  end
end

File.my_method

But in class methods you can't use instance variables of the File object. 但是在类方法中,不能使用File对象的实例变量。

You're trying to call my_method as a class method, but you've defined it as an instance method. 您试图将my_method调用为类方法,但已将其定义为实例方法。

You should either define it as def self.my_method , or create an instance of the controller to call it as an instance method. 您应该将其定义为def self.my_method ,或者创建控制器的实例以将其作为实例方法来调用。

In addition, you are going to run into problems for a couple of reasons - (1) Rails expects controllers to be named like FilesController , and (2) File is a class in the standard library. 此外,由于以下两个原因,您还会遇到问题-(1)Rails期望控制器的命名类似于FilesController ,并且(2) File是标准库中的类。 I would encourage you to change the class name to FilesController , and rename the file itself to files_controller.rb to prevent both issues. 我会鼓励你的类名称更改为FilesController ,并重新命名文件本身files_controller.rb ,以防止这两个问题。

Well... there are several interesting things going on with this example. 嗯,这个例子有很多有趣的事情。 The first would be that this class name is call File which is already defined in Ruby. 第一个是该类名称称为File ,它已经在Ruby中定义。

That is most likely why when you are in the console you didn't get an undefined class error. 这很可能就是为什么当您在控制台中时,没有得到未定义的类错误的原因。 Since my_method is not defined on Ruby's File class, this is why you are seeing undefined method. 由于my_method尚未在Ruby的File类上定义,因此这就是您看到未定义方法的原因。

Now to your question. 现在到您的问题。 I would try naming your class something different first and trying again from lib . 我会先为您的班级命名,然后再从lib重命名。 I believe it should be loaded by default again with the rails environment. 我相信默认情况下应该在rails环境中再次加载它。 For a version or two that functionality was taken out but I want to say it's back in. If not, just go into your config/application.rb file and look for a declaration along the lines of config.autoload_paths . 对于一个或两个版本,该功能已被删除,但我想说回来了。如果没有,只需进入config/application.rb文件,并沿着config.autoload_paths寻找声明。 Add the lib directory there and you should be good to go. 在此处添加lib目录,您应该会很好。

Lastly, is there a reason you want a controller in lib ? 最后,您是否有理由要在lib使用控制器?

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

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