LoginSignup
31
11

More than 5 years have passed since last update.

【Rails・Ruby】知っておくと得するかもしれない小技

Last updated at Posted at 2018-09-11

はじめに

初めまして!
Railsのアプリケーションを開発していて、個人的に便利だなと思った小技を紹介していきます。

1.アノテーションコメント

アノテーションを利用することで、開発中にやり残した事・対応すべき事を忘れないようにメモできます。アノテーションとは注釈、注記といった意味です。

アノテーションと記載すべき事

TODO ・・・後に追加すべき不足している機能の記載
FIXME・・・修正が必要な壊れたコードの記載
OPTIMIZE・・・パフォーマンスの最適化をすべき箇所の記載
HACK・・・リファクタリングすべき箇所の記載
REVIEW・・・ レビューが必要な箇所の記載

さっそく使ってみよう

hoges_controller.rb
def index
  #TODO: hoge
end

def new
  #FIXME: fuga
end

コンソールにてrails notesと入力します。

#rails notesで抽出できるのはTODO,FIXME,OPTIMIZEのみ
>rails notes

#結果
app/controllers/hoges_controller.rb:
  * [21] [TODO] hoge

app/controllers/hoges_controller.rb:
  * [17] [FIXME] fuga

対象ファイル、行数( []の中の数字 )、アノテーション、コメントが出力されます。開発中にやり残した事・対応すべき事を、メモしておくと忘れる事がないので便利です。
HACK,REVIEWなどを表示させるには、環境変数 ANNOTATION にnotes:custom タスクを実行し追加します。


rails notes:custom ANNOTATION=REVIEW

特定のアノテーションを出力する

コンソール
#TODOのみ表示
> rails notes:todo
app/controllers/hoges_controller.rb:
 [7] "hoge"

#FIXMEのみ表示
> rails notes:fixme
app/controllers/hoges_controller.rb:
 [17] fuga

2.reload!

rails cで動作を確認しながらコードを書くことがあります。
コードを変更する度にコンソールを抜け、再びrails cをするのは手間なので、reload!を使います。

rails c
#コードの変更
> reload!
Reloading...
=> true

なお、reload!実行以前の変数に格納したオブジェクトはリロードされていないので注意する必要があります。

> user = User.first
User id: 1, email: "hoge@gmail.com", created_at: "2018-06-18 12:03:15", updated_at: "2018-06-18 12:03:27", name: "hoge", sex: nil, occupation: nil, hobby: nil, comment: nil, more_than_18: true, icon: nil, birthday: nil>
> reload!
Reloading...
=> true
> user
User id: 1, email: "hoge@gmail.com", created_at: "2018-06-18 12:03:15", updated_at: "2018-06-18 12:03:27", name: "hoge", sex: nil, occupation: nil, hobby: nil, comment: nil, more_than_18: true, icon: nil, birthday: nil>

3.source_location

ソースコードのファイル名と行番号を配列で返します。
調査対象のメソッドをいったんMethodオブジェクトに変換し、source_locationメソッドを使うことで定義されている場所を特定できます。

> User.method(:new).source_location
=> ["lib/active_record/inheritance.rb", 49]

自作メソッドにも使えます

> User.method(:user_search).source_location
=> ["app/models/user.rb", 80]

ライブラリのメソッドにも使えます

> User.method(:mount_uploader).source_location
=> ["carrierwave/orm/activerecord.rb", 12]

このメソッドどんな処理しているんだろう?って時に定義元を素早く教えてくれるのはすごく便利ですね。
source_locationメソッド以外にもMethodクラスには面白いメソッドがあります。興味あるよって方はこちら

4.methods

レシーバが持っているメソッドの名前を集めて配列にして返します。

> User.methods
=>[:before_remove_for_relationships?,
 :_create_callbacks,
 :before_remove_for_relationships=,
 :after_remove_for_relationships,
 :_update_callbacks,
 :after_remove_for_relationships?,
 :after_remove_for_relationships=,
 :_destroy_callbacks,

sortすることもできます

> User.methods.sort
=> [:!,
 :!=,
 :!~,
 :<,
 :<=,
 :<=>,
 :==,
 :===,
 :=~,
 :>,
 :>=,

grepを使い特定のメソッドを取得できる。

例)createを含むメソッド

> User.methods.grep(/create/)
 [:_create_callbacks,
 :create_unique_string,
 :_create_callbacks=,
 :_create_callbacks?,
 :before_create,
 :after_create,
 :around_create,
 :create,
 :create!,
 :find_or_create_by!,
 :create_with,
 :find_or_create_by,
 :first_or_create,
 :first_or_create!]

終わりに

お疲れ様でした(^_^)
最後まで読んでいただきありがとうございます。
まだまだ駆け出しなので、疑問点や修正点がございましたら、コメントを頂けると幸いです!

参考

rubyリファレンスマニュアル
Ruby on Rails 5アプリケーションプログラミング
ruby-style-guide

31
11
4

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
31
11