Help us understand the problem. What is going on with this article?

Rails インスタンスメソッド内部でselfを使うべきか

More than 3 years have passed since last update.

書き方のハナシです。

bbatsov/ruby-style-guideによると、

# bad
def ready?
  if self.last_reviewed_at > self.last_updated_at
    self.worker.update(self.content, self.options)
    self.status = :in_progress
  end
  self.status == :verified
end

# good
def ready?
  if last_reviewed_at > last_updated_at
    worker.update(content, options)
    self.status = :in_progress
  end
  status == :verified
end

必要でない時にはselfは書かないほうが主流のようです。

ですが、

Avoid self where not required. (It is only required when calling a self write accessor.) [link]

とあるように、write_accessorのときは明示的にself書く必要があります。

まぁ考えてみれば、write_accessorのときは、

method_name = some_value
variable_name = some_value

のように、変数定義なのか、メソッド呼び出しなのか区別つかないですモンね。だからself明示する必要ありますね。

とまぁそんなバグがあって、考えたので、ここに記しました。

finc
健康寿命を伸ばすアプリFiNCの開発・運営を行うモバイルヘルステクノロジーベンチャー
https://finc.com
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした