Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
4
Help us understand the problem. What are the problem?

More than 5 years have passed since last update.

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

書き方のハナシです。

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明示する必要ありますね。

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

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
4
Help us understand the problem. What are the problem?