Determine which value was selected












0















I have three numerical variables: on_the_ground, double_round_trip and point_to_point. The price function returns one of these based on some simple logic.



Below is how it currently works.



def price
return on_the_ground if date_range == 1

values = [
on_the_ground,
double_round_trip
]

if !turbo? && !vlj?
values.push(point_to_point)
end

values.compact.min
end


I'd like to have a function that can return a symbol based on which value should be returned. For example:



def price_name
return :on_the_ground if date_range == 1
... etc...
end









share|improve this question























  • A method can return a Symbol, just like the price_name method above does. What exactly is the issue?

    – Rohit Namjoshi
    Nov 20 '18 at 4:12











  • I need to get the name of the variable that values.compact.min chose.

    – zcreative
    Nov 20 '18 at 4:21











  • To do that you need a mapping between values and names. Use Hash to do that.

    – Rohit Namjoshi
    Nov 20 '18 at 5:01
















0















I have three numerical variables: on_the_ground, double_round_trip and point_to_point. The price function returns one of these based on some simple logic.



Below is how it currently works.



def price
return on_the_ground if date_range == 1

values = [
on_the_ground,
double_round_trip
]

if !turbo? && !vlj?
values.push(point_to_point)
end

values.compact.min
end


I'd like to have a function that can return a symbol based on which value should be returned. For example:



def price_name
return :on_the_ground if date_range == 1
... etc...
end









share|improve this question























  • A method can return a Symbol, just like the price_name method above does. What exactly is the issue?

    – Rohit Namjoshi
    Nov 20 '18 at 4:12











  • I need to get the name of the variable that values.compact.min chose.

    – zcreative
    Nov 20 '18 at 4:21











  • To do that you need a mapping between values and names. Use Hash to do that.

    – Rohit Namjoshi
    Nov 20 '18 at 5:01














0












0








0








I have three numerical variables: on_the_ground, double_round_trip and point_to_point. The price function returns one of these based on some simple logic.



Below is how it currently works.



def price
return on_the_ground if date_range == 1

values = [
on_the_ground,
double_round_trip
]

if !turbo? && !vlj?
values.push(point_to_point)
end

values.compact.min
end


I'd like to have a function that can return a symbol based on which value should be returned. For example:



def price_name
return :on_the_ground if date_range == 1
... etc...
end









share|improve this question














I have three numerical variables: on_the_ground, double_round_trip and point_to_point. The price function returns one of these based on some simple logic.



Below is how it currently works.



def price
return on_the_ground if date_range == 1

values = [
on_the_ground,
double_round_trip
]

if !turbo? && !vlj?
values.push(point_to_point)
end

values.compact.min
end


I'd like to have a function that can return a symbol based on which value should be returned. For example:



def price_name
return :on_the_ground if date_range == 1
... etc...
end






ruby algorithm logic






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 '18 at 3:31









zcreativezcreative

17




17













  • A method can return a Symbol, just like the price_name method above does. What exactly is the issue?

    – Rohit Namjoshi
    Nov 20 '18 at 4:12











  • I need to get the name of the variable that values.compact.min chose.

    – zcreative
    Nov 20 '18 at 4:21











  • To do that you need a mapping between values and names. Use Hash to do that.

    – Rohit Namjoshi
    Nov 20 '18 at 5:01



















  • A method can return a Symbol, just like the price_name method above does. What exactly is the issue?

    – Rohit Namjoshi
    Nov 20 '18 at 4:12











  • I need to get the name of the variable that values.compact.min chose.

    – zcreative
    Nov 20 '18 at 4:21











  • To do that you need a mapping between values and names. Use Hash to do that.

    – Rohit Namjoshi
    Nov 20 '18 at 5:01

















A method can return a Symbol, just like the price_name method above does. What exactly is the issue?

– Rohit Namjoshi
Nov 20 '18 at 4:12





A method can return a Symbol, just like the price_name method above does. What exactly is the issue?

– Rohit Namjoshi
Nov 20 '18 at 4:12













I need to get the name of the variable that values.compact.min chose.

– zcreative
Nov 20 '18 at 4:21





I need to get the name of the variable that values.compact.min chose.

– zcreative
Nov 20 '18 at 4:21













To do that you need a mapping between values and names. Use Hash to do that.

– Rohit Namjoshi
Nov 20 '18 at 5:01





To do that you need a mapping between values and names. Use Hash to do that.

– Rohit Namjoshi
Nov 20 '18 at 5:01












3 Answers
3






active

oldest

votes


















0














If I get your point, why not use an array?



def price_name_for date_range
[:on_the_ground, :double_round_trip, :point_to_point][date_range-1]
end

price_name_for 1 #=> :on_the_ground
price_name_for 2 #=> :double_round_trip
price_name_for 3 #=> :point_to_point





share|improve this answer
























  • It's a little more complicated than that. There are some conditions for the point to point price to be in the array. I've provided an answer below showing how I solved it.

    – zcreative
    Nov 20 '18 at 7:14



















-1














you could use case



case values
when double_round_trip
return :double_round_trip
when on_the_ground
...


https://www.rubyguides.com/2015/10/ruby-case/






share|improve this answer































    -1














    I ended up using:



    price_options.key(price) # double_round_trip





    share|improve this answer
























    • Using a Hash is what I suggested in my comment.

      – Rohit Namjoshi
      Nov 20 '18 at 15:42











    Your Answer






    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: "1"
    };
    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',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    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%2fstackoverflow.com%2fquestions%2f53385814%2fdetermine-which-value-was-selected%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    If I get your point, why not use an array?



    def price_name_for date_range
    [:on_the_ground, :double_round_trip, :point_to_point][date_range-1]
    end

    price_name_for 1 #=> :on_the_ground
    price_name_for 2 #=> :double_round_trip
    price_name_for 3 #=> :point_to_point





    share|improve this answer
























    • It's a little more complicated than that. There are some conditions for the point to point price to be in the array. I've provided an answer below showing how I solved it.

      – zcreative
      Nov 20 '18 at 7:14
















    0














    If I get your point, why not use an array?



    def price_name_for date_range
    [:on_the_ground, :double_round_trip, :point_to_point][date_range-1]
    end

    price_name_for 1 #=> :on_the_ground
    price_name_for 2 #=> :double_round_trip
    price_name_for 3 #=> :point_to_point





    share|improve this answer
























    • It's a little more complicated than that. There are some conditions for the point to point price to be in the array. I've provided an answer below showing how I solved it.

      – zcreative
      Nov 20 '18 at 7:14














    0












    0








    0







    If I get your point, why not use an array?



    def price_name_for date_range
    [:on_the_ground, :double_round_trip, :point_to_point][date_range-1]
    end

    price_name_for 1 #=> :on_the_ground
    price_name_for 2 #=> :double_round_trip
    price_name_for 3 #=> :point_to_point





    share|improve this answer













    If I get your point, why not use an array?



    def price_name_for date_range
    [:on_the_ground, :double_round_trip, :point_to_point][date_range-1]
    end

    price_name_for 1 #=> :on_the_ground
    price_name_for 2 #=> :double_round_trip
    price_name_for 3 #=> :point_to_point






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 20 '18 at 6:12









    iGianiGian

    4,2692623




    4,2692623













    • It's a little more complicated than that. There are some conditions for the point to point price to be in the array. I've provided an answer below showing how I solved it.

      – zcreative
      Nov 20 '18 at 7:14



















    • It's a little more complicated than that. There are some conditions for the point to point price to be in the array. I've provided an answer below showing how I solved it.

      – zcreative
      Nov 20 '18 at 7:14

















    It's a little more complicated than that. There are some conditions for the point to point price to be in the array. I've provided an answer below showing how I solved it.

    – zcreative
    Nov 20 '18 at 7:14





    It's a little more complicated than that. There are some conditions for the point to point price to be in the array. I've provided an answer below showing how I solved it.

    – zcreative
    Nov 20 '18 at 7:14













    -1














    you could use case



    case values
    when double_round_trip
    return :double_round_trip
    when on_the_ground
    ...


    https://www.rubyguides.com/2015/10/ruby-case/






    share|improve this answer




























      -1














      you could use case



      case values
      when double_round_trip
      return :double_round_trip
      when on_the_ground
      ...


      https://www.rubyguides.com/2015/10/ruby-case/






      share|improve this answer


























        -1












        -1








        -1







        you could use case



        case values
        when double_round_trip
        return :double_round_trip
        when on_the_ground
        ...


        https://www.rubyguides.com/2015/10/ruby-case/






        share|improve this answer













        you could use case



        case values
        when double_round_trip
        return :double_round_trip
        when on_the_ground
        ...


        https://www.rubyguides.com/2015/10/ruby-case/







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 20 '18 at 4:19









        Hector Moreno-BravoHector Moreno-Bravo

        12




        12























            -1














            I ended up using:



            price_options.key(price) # double_round_trip





            share|improve this answer
























            • Using a Hash is what I suggested in my comment.

              – Rohit Namjoshi
              Nov 20 '18 at 15:42
















            -1














            I ended up using:



            price_options.key(price) # double_round_trip





            share|improve this answer
























            • Using a Hash is what I suggested in my comment.

              – Rohit Namjoshi
              Nov 20 '18 at 15:42














            -1












            -1








            -1







            I ended up using:



            price_options.key(price) # double_round_trip





            share|improve this answer













            I ended up using:



            price_options.key(price) # double_round_trip






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 20 '18 at 5:09









            zcreativezcreative

            17




            17













            • Using a Hash is what I suggested in my comment.

              – Rohit Namjoshi
              Nov 20 '18 at 15:42



















            • Using a Hash is what I suggested in my comment.

              – Rohit Namjoshi
              Nov 20 '18 at 15:42

















            Using a Hash is what I suggested in my comment.

            – Rohit Namjoshi
            Nov 20 '18 at 15:42





            Using a Hash is what I suggested in my comment.

            – Rohit Namjoshi
            Nov 20 '18 at 15:42


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53385814%2fdetermine-which-value-was-selected%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







            這個網誌中的熱門文章

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud

            Zucchini