My code looks like this, and it works:
if Target.find_by_shrunk(params[:shrunk])
@target = Target.find_by_shrunk(params[:shrunk])
else
# do something else
end
Target::find_by_shrunk(params[:shrunk]) gets called twice. In order to avoid this, I want to run Target.find_by_shrunk(params[:shrunk]) once, catch the true/false result, then use that variable in the conditional statement. I tried doing this:
does_it_exist = (this_target = Target.find_by_shrunk(params[:shrunk]))
if does_it_exist
@target = this_target
else
# do something else
end
But unfortunately that doesn’t do what I want it to.
How do I simultaneously perform an assignment, and somehow capture the true/false result of performing that assignment, so i don’t have to run the same (expensive) piece of code twice in a row?
You could do
unlessis the same asif not. An attribution returns the value attributed, and if it’s different fromnilandfalse, it evaluates totrue.