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\$
1
  • \$\begingroup\$ I'm really unsure what your goal is here. Can you clarify? \$\endgroup\$ Jul 20, 2018 at 2:59

1 Answer 1

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
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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