Skip to content

Loading…

Avoid self #104

Merged
merged 1 commit into from

4 participants

@philc

Looks good to me.

@bbatsov bbatsov merged commit 8f28565 into bbatsov:master
@JuanitoFatas JuanitoFatas added a commit to JuanitoFatas/ruby-style-guide that referenced this pull request
@JuanitoFatas JuanitoFatas 更新 #104 20527f0
@dsferreira

Please see my comment in: issue 97

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 5, 2012
  1. @marcandre

    Avoid self

    marcandre committed
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
Something went wrong with that request. Please try again.