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
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It only takes a minute to sign up.
Sign up to join this communityI'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
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