Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #104 from marcandre/avoid_self

Avoid self
  • Loading branch information...
commit 8f2856577cc86aacea740b309a999dc7fc83df4c 2 parents 72aa158 + 1389452
@bbatsov bbatsov authored
Showing with 49 additions and 0 deletions.
  1. +49 −0 README.md
View
49 README.md
@@ -479,6 +479,55 @@ You can generate a PDF or an HTML copy of this guide using
end
```
+* Avoid `self` where not required.
+
+ ```Ruby
+ # 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
+ ```
+
+* As a corollary, avoid shadowing methods with local variables unless they are both equivalent
+
+ ```Ruby
+ class Foo
+ attr_accessor :options
+
+ # ok
+ def initialize(options)
+ self.options = options
+ # both options and self.options are equivalent here
+ end
+
+ # bad
+ def do_something(options = {})
+ unless options[:when] == :later
+ output(self.options[:message])
+ end
+ end
+
+ # good
+ def do_something(params = {})
+ unless params[:when] == :later
+ output(options[:message])
+ end
+ end
+ end
+
* Use spaces around the `=` operator when assigning default values to method parameters:
```Ruby
Please sign in to comment.
Something went wrong with that request. Please try again.