如何使用Ruby on Rails 3创建和使用模块?

时间:2021-06-20 23:17:11

I am using Ruby on Rails 3 and I would like to move some custom and shared code in a module.

我正在使用Ruby on Rails 3,我想在模块中移动一些自定义和共享代码。

  1. What syntax should I use to write the module code?
  2. 我应该用什么语法来编写模块代码?
  3. In which folder of my application I have to place the module file?
  4. 在我的应用程序的哪个文件夹中,我必须放置模块文件?
  5. How I have to include that module in one or more controller classes?
  6. 我如何在一个或多个控制器类中包含该模块?
  7. What other action, if any, do I have to use the custom module anywhere in my application?
  8. 如果有的话,我还需要在我的应用程序中的任何位置使用自定义模块吗?
  9. How can I call methods in the module from my application?
  10. 如何从我的应用程序中调用模块中的方法?

Thanks in advance.

提前致谢。

2 个解决方案

#1


110  

To 1. A module is created/opened by simply saying:

要1.通过简单地说:创建/打开模块:

module MyModule
  def first_module_method
  end
end

To 2. The lib folder. If you want to organize your modules in the lib folder, you can put them into modules themselves. For example, if you wanted a subfolder super_modules your modules would be defined as follows:

至2. lib文件夹。如果要在lib文件夹中组织模块,可以将它们自己放入模块中。例如,如果您想要一个子文件夹super_modules,您的模块将定义如下:

module SuperModules
  module MyModule
    def first_module_method
    end
  end
end

To 3./5. When including the module in a class you can simply call the modules methods as if they were defined within the class:

至3./5。将模块包含在类中时,您只需调用模块方法,就好像它们是在类中定义的一样:

class MyClass
  include MyModule
  def some_method
    first_module_method #calls module method
  end
end

To 4. Frst, make sure that your module is really needed in every class of your application. If it isn't it makes sense to only include it where it is need so as not to bloat the classes that don't need it anyways. If you really want the module everywhere, include look at the class hierarchy of your classes in the app. Do you want the module in all models? You could open ActiveRecord::Base and add add your module there.

要4.首先,确保在您的应用程序的每个类中确实需要您的模块。如果不是这样,只将它包含在需要的地方是有意义的,以免膨胀不需要它的类。如果你真的想在任何地方使用该模块,请在应用程序中查看类的类层次结构。你想要所有型号的模块吗?您可以打开ActiveRecord :: Base并在那里添加您的模块。

#2


3  

A>1. You can use the same syntax as any other ruby class. For instance, I'm defining a VehicleClassifer module which is going to use the classify_vehicle method to classify a vehicle based on the number of wheels it receives as an input.

A> 1。您可以使用与任何其他ruby类相同的语法。例如,我正在定义一个VehicleClassifer模块,该模块将使用classify_vehicle方法根据车辆接收的车轮数量作为输入对车辆进行分类。

module VehicleClassifer
  def classify_vehicle(number_of_wheels)
    VehicleType.where("number_of_wheels = ?", number_of_wheels)
  end
end

A>2. Modules are usually stored in the /lib folder.

A> 2。模块通常存储在/ lib文件夹中。

questions 3,4,5 have more or less the same answer. you can use

问题3,4,5或多或少有相同的答案。您可以使用

class SomeController < ApplicationController
  include VehicleClassfier

  def index 
    classify_vehicle(4)  
  end
end

in the class you're using the module and you will have access to all the module's methods.

在您正在使用该模块的类中,您将可以访问所有模块的方法。

Also, In case you need to use a module through out your app, you can include it in your application controller.

此外,如果您需要在整个应用程序中使用模块,可以将其包含在应用程序控制器中。

#1


110  

To 1. A module is created/opened by simply saying:

要1.通过简单地说:创建/打开模块:

module MyModule
  def first_module_method
  end
end

To 2. The lib folder. If you want to organize your modules in the lib folder, you can put them into modules themselves. For example, if you wanted a subfolder super_modules your modules would be defined as follows:

至2. lib文件夹。如果要在lib文件夹中组织模块,可以将它们自己放入模块中。例如,如果您想要一个子文件夹super_modules,您的模块将定义如下:

module SuperModules
  module MyModule
    def first_module_method
    end
  end
end

To 3./5. When including the module in a class you can simply call the modules methods as if they were defined within the class:

至3./5。将模块包含在类中时,您只需调用模块方法,就好像它们是在类中定义的一样:

class MyClass
  include MyModule
  def some_method
    first_module_method #calls module method
  end
end

To 4. Frst, make sure that your module is really needed in every class of your application. If it isn't it makes sense to only include it where it is need so as not to bloat the classes that don't need it anyways. If you really want the module everywhere, include look at the class hierarchy of your classes in the app. Do you want the module in all models? You could open ActiveRecord::Base and add add your module there.

要4.首先,确保在您的应用程序的每个类中确实需要您的模块。如果不是这样,只将它包含在需要的地方是有意义的,以免膨胀不需要它的类。如果你真的想在任何地方使用该模块,请在应用程序中查看类的类层次结构。你想要所有型号的模块吗?您可以打开ActiveRecord :: Base并在那里添加您的模块。

#2


3  

A>1. You can use the same syntax as any other ruby class. For instance, I'm defining a VehicleClassifer module which is going to use the classify_vehicle method to classify a vehicle based on the number of wheels it receives as an input.

A> 1。您可以使用与任何其他ruby类相同的语法。例如,我正在定义一个VehicleClassifer模块,该模块将使用classify_vehicle方法根据车辆接收的车轮数量作为输入对车辆进行分类。

module VehicleClassifer
  def classify_vehicle(number_of_wheels)
    VehicleType.where("number_of_wheels = ?", number_of_wheels)
  end
end

A>2. Modules are usually stored in the /lib folder.

A> 2。模块通常存储在/ lib文件夹中。

questions 3,4,5 have more or less the same answer. you can use

问题3,4,5或多或少有相同的答案。您可以使用

class SomeController < ApplicationController
  include VehicleClassfier

  def index 
    classify_vehicle(4)  
  end
end

in the class you're using the module and you will have access to all the module's methods.

在您正在使用该模块的类中,您将可以访问所有模块的方法。

Also, In case you need to use a module through out your app, you can include it in your application controller.

此外,如果您需要在整个应用程序中使用模块,可以将其包含在应用程序控制器中。