我如何获得模型的方法名称数组?

maz*_*yry 3 ruby ruby-on-rails

Rails/Ruby中是否有可能获取模型方法的列表.FE.ModelName.methods

我想获得属于Mailer模型的所有方法名称.

kik*_*kik 6

排序方法总是在ruby中是一个问题,因为你不能简单地说:"给我特定于该类的方法".

获取特定类的方法

您必须使用@Monk_Code提到的数组减法,即便如此,您也无法将方法与基本实现和猴子补丁分开.

要彻底删除所有包含的模块和所有父项方法:

> MyClass.instance_methods - ( MyClass.ancestors - [ MyClass ] ).map( &:instance_methods ).flatten
Run Code Online (Sandbox Code Playgroud)

更换#instance_methods#methods,如果你想通过这门课的方法.

请注意,#define_method在父类(如模型回调)中动态创建的方法仍将显示,因为它们直接在子类上定义.

获取特定文件的方法

通常,从类中隔离方法是不够的.

我写了一个帮助我帮助我从文件中隔离方法的助手.它允许这样做:

> MyModel.new.located_methods
+------------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------+
|                 Name                   |                                                          Location                                                          |
+------------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------+
| !                                      |                                                                                                                            |
| <=>                                    | /home/user/.gem/ruby/2.0.0/gems/activerecord-4.0.0/lib/active_record/core.rb line 324                                       |
| unloadable                             | /home/user/.gem/ruby/2.0.0/gems/activesupport-4.0.0/lib/active_support/dependencies.rb line 245                             |
| ==                                     | /home/user/.gem/ruby/2.0.0/gems/activerecord-4.0.0/lib/active_record/core.rb line 296                                       |
| validates_format_of                    | /home/user/.gem/ruby/2.0.0/gems/activemodel-4.0.0/lib/active_model/validations/format.rb line 110                           |

| and so on ... |
Run Code Online (Sandbox Code Playgroud)

第一个参数允许grep方法名称:

> MyModel.new.located_methods /validate/
+----------------------------------------------------------+----------------------------------------------------------------------------------------------------------------------+
|                        Name                |                                                       Location                                                       |
+----------------------------------------------------------+----------------------------------------------------------------------------------------------------------------------+
| _validate_callbacks                        | /home/user/.gem/ruby/2.0.0/gems/activesupport-4.0.0/lib/active_support/core_ext/class/attribute.rb line 107           |
| _validate_callbacks=                       | /home/user/.gem/ruby/2.0.0/gems/activesupport-4.0.0/lib/active_support/core_ext/class/attribute.rb line 117           |
| _validate_callbacks?                       | /home/user/.gem/ruby/2.0.0/gems/activesupport-4.0.0/lib/active_support/core_ext/class/attribute.rb line 114           |
| validate_associated_records_for_amenities  | /home/user/.gem/ruby/2.0.0/gems/activerecord-4.0.0/lib/active_record/autosave_association.rb line 147                 |
Run Code Online (Sandbox Code Playgroud)

第二个允许每个源文件grep:

> MyModel.new.located_methods /validate/, /autosave/
+----------------------------------------------------------+------------------------------------------------------------------------------------------------------+
|                        Name                |                                               Location                                               |
+----------------------------------------------------------+------------------------------------------------------------------------------------------------------+
| validate_associated_records_for_amenities  | /home/user/.gem/ruby/2.0.0/gems/activerecord-4.0.0/lib/active_record/autosave_association.rb line 147 |
Run Code Online (Sandbox Code Playgroud)

代码

class Object
  # Display an object methods list with their source location.
  #
  # List can optionally be filtered by method pattern and source file
  # pattern.
  #
  # Mainly useful for debugging.
  #
  # @param [Regexp] method_pattern grep method name
  # @param [Regexp] file_pattern grep file name
  def located_methods( method_pattern = nil, file_pattern = nil )
    list = ( method_pattern ? methods.grep( method_pattern ) : methods ).sort.map do |name|
      location = method( name ).source_location
      location = "#{location.first} line #{location.second}" if location
      [ name.to_s.colorize( :yellow ), location ]
    end

    list = list.select { |meth| meth[1] =~ file_pattern } if file_pattern

    puts ( [[ 'Name'.colorize( :yellow ), 'Location' ]] + list ).to_table( first_row_is_head: true )
    true
  end
end
Run Code Online (Sandbox Code Playgroud)

此版本取决于colorizetext-table,但您可以轻松修改它以使用您首选的格式化方式.


Зел*_*ный 5

class Object
  def show_methods
    (methods - self.class.superclass.instance_methods).sort
  end
end

Mailer.show_methods
Run Code Online (Sandbox Code Playgroud)

例如