Ensure that a Sinatra hash always has a valid value

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
0
down vote

favorite












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






share|improve this question



















  • I'm really unsure what your goal is here. Can you clarify?
    – thesecretmaster
    Jul 20 at 2:59
















up vote
0
down vote

favorite












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






share|improve this question



















  • I'm really unsure what your goal is here. Can you clarify?
    – thesecretmaster
    Jul 20 at 2:59












up vote
0
down vote

favorite









up vote
0
down vote

favorite











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






share|improve this question











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








share|improve this question










share|improve this question




share|improve this question









asked Jun 26 at 19:41









craig

1085




1085











  • I'm really unsure what your goal is here. Can you clarify?
    – thesecretmaster
    Jul 20 at 2:59
















  • I'm really unsure what your goal is here. Can you clarify?
    – thesecretmaster
    Jul 20 at 2:59















I'm really unsure what your goal is here. Can you clarify?
– thesecretmaster
Jul 20 at 2:59




I'm really unsure what your goal is here. Can you clarify?
– thesecretmaster
Jul 20 at 2:59










1 Answer
1






active

oldest

votes

















up vote
0
down vote













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





share|improve this answer





















    Your Answer




    StackExchange.ifUsing("editor", function ()
    return StackExchange.using("mathjaxEditing", function ()
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
    );
    );
    , "mathjax-editing");

    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "196"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );








     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f197306%2fensure-that-a-sinatra-hash-always-has-a-valid-value%23new-answer', 'question_page');

    );

    Post as a guest






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    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





    share|improve this answer

























      up vote
      0
      down vote













      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





      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        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





        share|improve this answer













        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






        share|improve this answer













        share|improve this answer



        share|improve this answer











        answered Jul 22 at 23:37









        David Maze

        1011




        1011






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f197306%2fensure-that-a-sinatra-hash-always-has-a-valid-value%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            Python Lists

            Aion

            JavaScript Array Iteration Methods