Cascading Ruby Logic? -
is possible following signed out user without triggering .consumer
being called on nil error?
if signed_in? && current_user.consumer puts "hello world" end
for of site have had nest these types of conditionals like:
if signed_in? if current_user.consumer end end
...which doesn't bad, when have lots of other code on page, can hassle..
i want statement signed_in?
evaluated , if true second conditional evaluated... in single line. possible?
yes! ruby supports short-circuiting logic, first code block want: if signed_in?
evaluates false
, ruby interpreter lazy , not bother evaluating rest of condition.
similarly, if user not signed in, !signed_in? || current_user.banned?
short-circuit when realizes !signed_in?
true
, not bother evaluate current_user.banned?
.
Comments
Post a Comment