1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

rails consoleでメソッドの一覧や定義場所を確認する

Posted at

はじめに

Railsでコードを書いているときにメソッドの有無や定義場所を調べたい!となることがあったので、方法をまとめておきます。

メソッドの一覧を確認する

メソッドの一覧はmethodsメソッドで確認できます。
使用可能なメソッドがシンボルの配列として返ってきます。

[1] pry(main)> user = UserController.new()
=> #<UserController:0x000000000069a0>
[2] pry(main)> user.methods
=> [:index,
 :show,
 :create,
 :update,
# ~省略~

メソッドの定義場所を確認する

methodメソッドとMetho#source_locationメソッドで確認できます。
0番目の要素に定義ファイルの絶対パスが、1番目に行数が入った配列が返ってきます。

[3] pry(main)> user.method(:index).source_location
=> ["/home/user/workspace/sample_app/app/controllers/users_controller.rb", 5]
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?