Let’s say I want to do this pseudo code:
if plan = some_api.get_plan
return plan
else
return 'free'
end
How can I most elegantly do this in Rails? I want to minimise calls to some_api.get_plan.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
In this case the simplest code is:
But since assignments return the assigned value, you can do assignment and a conditional at the same time (and in fact, I do it all the time to make my code more concise).