1
\$\begingroup\$

I'm trying to ensure that a Sinatra hash always has a valid value.

Is there a more-concise way to write this?

params[:v] = if [:icons,:list].include? (params[:v] ||= :list).downcase.to_sym then (params[:v] ||= :list).downcase.to_sym else :list end
\$\endgroup\$
  • \$\begingroup\$ I'm really unsure what your goal is here. Can you clarify? \$\endgroup\$ – thesecretmaster Jul 20 '18 at 2:59
1
\$\begingroup\$

I'd write this in multiple lines, so that you don't have to write out the complex conversion multiple times:

v = (params[:v] || :list).downcase.to_sym
params[:v] = if %i[icons list].include?(v) then v else :list end
| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.