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

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

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

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

获取特定类的方法

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

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

> MyClass.instance_methods - ( MyClass.ancestors - [ MyClass ] ).map( &:instance_methods ).flatten 

如果需要类方法,请将#instance_methods替换为#instance_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 ... | 

第一个参数允许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 | 

第二个允许每个源文件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 | 

代码

 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 

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

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

例如

我建议使用

 show-method 

来自撬gem 。

您还可以在对象中导航,就像在unix控制台中一样

 cd ls 

等等…

看看这个小例子

这个: http : //pryrepl.org/