Ensure that a Sinatra hash always has a valid value











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














bumped to the homepage by Community yesterday


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.















  • 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














bumped to the homepage by Community yesterday


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.















  • 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






ruby sinatra






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jun 26 at 19:41









craig

1085




1085





bumped to the homepage by Community yesterday


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







bumped to the homepage by Community yesterday


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.














  • 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: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    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















    Required, but never shown

























    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















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Costa Masnaga

            Fotorealismo

            Sidney Franklin