我有一个Test
在lib
文件夹中的类,我想在我的控制器类名的方法中动态地创建这个类的一个对象class UsersController < ApplicationController
。我怎样才能做到这一点?
就像是
class UsersController < ApplicationController
def address
{"here I want to create the object of Test class"}
end
end
我有一个Test
在lib
文件夹中的类,我想在我的控制器类名的方法中动态地创建这个类的一个对象class UsersController < ApplicationController
。我怎样才能做到这一点?
就像是
class UsersController < ApplicationController
def address
{"here I want to create the object of Test class"}
end
end
在 Ruby 中,您不能像在 C++ 中那样创建对象,您只能使用Object::new
方法创建动态对象。Test
所以要创建你简单的类的实例:
test = Test.new
ruby 中没有编译。如果在 c 的主范围中定义了 Test ,那么它只是一个常量,它可以在任何地方使用。
Ruby 是解释性语言,因此没有编译阶段。在 config/application.rb 加载 lib 文件夹或文件,因此在启动时它会自动加载这些文件。现在,如果您想要所有方法的测试类实例,只需创建创建实例并在使用方法之前加载的方法before_filter