Skip to content

Commit 98292e2

Browse files
committed
Tweak find-by-memoization good example
The current good section is quite a mouthful. That is partially the fault of Ruby, but it can be better: * Less indentation * Fewer lines of code * More concise This style is quite common: https://github.com/search?q=lang%3Aruby%20%2Freturn%20.*%20if%20defined%2F&type=code Tried searching for the other one but every search I try just times out. As for `instance_variable_defined?` vs `defined?`, it's just shorter.
1 parent b2b5987 commit 98292e2

File tree

1 file changed

+3
-5
lines changed

1 file changed

+3
-5
lines changed

README.adoc

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1160,11 +1160,9 @@ end
11601160
11611161
# good
11621162
def current_user
1163-
if instance_variable_defined?(:@current_user)
1164-
@current_user
1165-
else
1166-
@current_user = User.find_by(id: session[:user_id])
1167-
end
1163+
return @current_user if defined?(@current_user)
1164+
1165+
@current_user = User.find_by(id: session[:user_id])
11681166
end
11691167
----
11701168

0 commit comments

Comments
 (0)