Rails/Ruby でモデルのメソッドのリストを取得する可能性はありますか。フェ。ModelName.methods
Mailer モデルに属するすべてのメソッドの名前を取得したいと考えています。
Rails/Ruby でモデルのメソッドのリストを取得する可能性はありますか。フェ。ModelName.methods
Mailer モデルに属するすべてのメソッドの名前を取得したいと考えています。
Ruby ではメソッドの選別が常に問題になります。「そのクラスに固有のメソッドをください」と単純に言うことはできないからです。
@Monk_Code で言及されているような配列の減算を使用する必要があります。それでも、基本実装とそのモンキー パッチからメソッドを分離することはできません。
含まれているすべてのモジュールとすべての親メソッドを徹底的に削除するには:
> MyClass.instance_methods - ( MyClass.ancestors - [ MyClass ] ).map( &:instance_methods ).flatten
#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 ... |
最初のパラメータにより、メソッド名を 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 |
2 つ目は、ソース ファイルごとに 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に依存しますが、好みの書式設定方法を使用するように簡単に変更できます。
を使用することをお勧めします
show-method
詮索好きな宝石から。
UNIXコンソールのようにオブジェクト内をナビゲートすることもできます
cd
ls
等...
この小さな例をチェックしてください
そしてこれ:http://pryrepl.org/