Skip to content

Commit

Permalink
Use a leading underscore for memoization
Browse files Browse the repository at this point in the history
When using an instance variable prefix the variable with an underscore
to discourage direct use of the ivar over the method.
  • Loading branch information
BlakeWilliams committed Nov 16, 2015
1 parent 90774b4 commit 50f9b15
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 0 deletions.
1 change: 1 addition & 0 deletions style/ruby/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Ruby
* Prefer `if` over `unless`.
* Use `_` for unused block parameters.
* Prefix unused variables or parameters with underscore (`_`).
* Use a leading underscore when defining instance variables for memoization.
* Use `%{}` for single-line strings needing interpolation and double-quotes.
* Use `{...}` for single-line blocks. Use `do..end` for multi-line blocks.
* Use `?` suffix for predicate methods.
Expand Down
4 changes: 4 additions & 0 deletions style/ruby/sample.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ def self.class_method
method_body
end

def memoized_method
@_memoized_method ||= 1
end

protected

attr_reader :foo
Expand Down

0 comments on commit 50f9b15

Please sign in to comment.