Partition array to hash in ruby











up vote
1
down vote

favorite












Problem: convert this array of hashes:



cars = [
{ :model=>"Ferrari 458", :speed=>320 },
{ :model=>"Maserati MC12", :speed=>330 },
{ :model=>"Ferrari Enzo", :speed=>350 },
{ :model=>"Lamborghini Huracan", :speed=>325 }
]


to this data structure:



{
above_320: [
{ :model=>"Maserati MC12", :speed=>330 },
{ :model=>"Lamborghini Huracan", :speed=>325 },
{ :model=>"Ferrari Enzo", :speed=>350 }
],
the_rest: [
{ :model=>"Ferrari 458", :speed=>320 }
]
}


My solution:



cars.partition {|car| car[:speed] > 320}
.map.with_index {|cars,i| [ i == 0 ? :above_320 : :the_rest, cars ]}
.to_h


Any feedback would be greatly appreciated!










share|improve this question







New contributor




sekmo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
























    up vote
    1
    down vote

    favorite












    Problem: convert this array of hashes:



    cars = [
    { :model=>"Ferrari 458", :speed=>320 },
    { :model=>"Maserati MC12", :speed=>330 },
    { :model=>"Ferrari Enzo", :speed=>350 },
    { :model=>"Lamborghini Huracan", :speed=>325 }
    ]


    to this data structure:



    {
    above_320: [
    { :model=>"Maserati MC12", :speed=>330 },
    { :model=>"Lamborghini Huracan", :speed=>325 },
    { :model=>"Ferrari Enzo", :speed=>350 }
    ],
    the_rest: [
    { :model=>"Ferrari 458", :speed=>320 }
    ]
    }


    My solution:



    cars.partition {|car| car[:speed] > 320}
    .map.with_index {|cars,i| [ i == 0 ? :above_320 : :the_rest, cars ]}
    .to_h


    Any feedback would be greatly appreciated!










    share|improve this question







    New contributor




    sekmo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      Problem: convert this array of hashes:



      cars = [
      { :model=>"Ferrari 458", :speed=>320 },
      { :model=>"Maserati MC12", :speed=>330 },
      { :model=>"Ferrari Enzo", :speed=>350 },
      { :model=>"Lamborghini Huracan", :speed=>325 }
      ]


      to this data structure:



      {
      above_320: [
      { :model=>"Maserati MC12", :speed=>330 },
      { :model=>"Lamborghini Huracan", :speed=>325 },
      { :model=>"Ferrari Enzo", :speed=>350 }
      ],
      the_rest: [
      { :model=>"Ferrari 458", :speed=>320 }
      ]
      }


      My solution:



      cars.partition {|car| car[:speed] > 320}
      .map.with_index {|cars,i| [ i == 0 ? :above_320 : :the_rest, cars ]}
      .to_h


      Any feedback would be greatly appreciated!










      share|improve this question







      New contributor




      sekmo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      Problem: convert this array of hashes:



      cars = [
      { :model=>"Ferrari 458", :speed=>320 },
      { :model=>"Maserati MC12", :speed=>330 },
      { :model=>"Ferrari Enzo", :speed=>350 },
      { :model=>"Lamborghini Huracan", :speed=>325 }
      ]


      to this data structure:



      {
      above_320: [
      { :model=>"Maserati MC12", :speed=>330 },
      { :model=>"Lamborghini Huracan", :speed=>325 },
      { :model=>"Ferrari Enzo", :speed=>350 }
      ],
      the_rest: [
      { :model=>"Ferrari 458", :speed=>320 }
      ]
      }


      My solution:



      cars.partition {|car| car[:speed] > 320}
      .map.with_index {|cars,i| [ i == 0 ? :above_320 : :the_rest, cars ]}
      .to_h


      Any feedback would be greatly appreciated!







      ruby






      share|improve this question







      New contributor




      sekmo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question







      New contributor




      sekmo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question






      New contributor




      sekmo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked yesterday









      sekmo

      1135




      1135




      New contributor




      sekmo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      sekmo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      sekmo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          3
          down vote



          accepted










          I would go with the chunk method:



          cars.chunk { |c| c[:speed]>320 ? :above_320 : :the_rest }.to_h


          Though it's important to underline that chunk doesn't work if the array is not ordered (by speed in this case). In that case we need to prepend it with sort_by:



          cars.sort_by {|car| car[:speed]}.chunk { |c| c[:speed]>320 ? :above_320 : :the_rest }.to_h


          (Moreover, if we want to order the hash keys, like in the example, we need to call sort after chunk):



          cars.sort_by {|car| car[:speed]}.chunk { |c| c[:speed]>320 ? :above_320 : :the_rest }.sort.to_h





          share|improve this answer























          • Thanks! It's a thing that popped up from a pet project actually :-)
            – sekmo
            yesterday




















          up vote
          1
          down vote













          I think that .map.with_index {|cars,i| [ i == 0 ? :above_320 : :the_rest, cars ]} is a bit more verbose and awkward than it needs to be.



          Hash[
          [:above_320, :the_rest].zip(cars.partition { |car| car[:speed] > 320 })
          ]


          Alternatively,



          [:above_320, :the_rest]
          .zip(cars.partition { |car| car[:speed] > 320 })
          .to_h





          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
            });


            }
            });






            sekmo is a new contributor. Be nice, and check out our Code of Conduct.










             

            draft saved


            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f207607%2fpartition-array-to-hash-in-ruby%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            3
            down vote



            accepted










            I would go with the chunk method:



            cars.chunk { |c| c[:speed]>320 ? :above_320 : :the_rest }.to_h


            Though it's important to underline that chunk doesn't work if the array is not ordered (by speed in this case). In that case we need to prepend it with sort_by:



            cars.sort_by {|car| car[:speed]}.chunk { |c| c[:speed]>320 ? :above_320 : :the_rest }.to_h


            (Moreover, if we want to order the hash keys, like in the example, we need to call sort after chunk):



            cars.sort_by {|car| car[:speed]}.chunk { |c| c[:speed]>320 ? :above_320 : :the_rest }.sort.to_h





            share|improve this answer























            • Thanks! It's a thing that popped up from a pet project actually :-)
              – sekmo
              yesterday

















            up vote
            3
            down vote



            accepted










            I would go with the chunk method:



            cars.chunk { |c| c[:speed]>320 ? :above_320 : :the_rest }.to_h


            Though it's important to underline that chunk doesn't work if the array is not ordered (by speed in this case). In that case we need to prepend it with sort_by:



            cars.sort_by {|car| car[:speed]}.chunk { |c| c[:speed]>320 ? :above_320 : :the_rest }.to_h


            (Moreover, if we want to order the hash keys, like in the example, we need to call sort after chunk):



            cars.sort_by {|car| car[:speed]}.chunk { |c| c[:speed]>320 ? :above_320 : :the_rest }.sort.to_h





            share|improve this answer























            • Thanks! It's a thing that popped up from a pet project actually :-)
              – sekmo
              yesterday















            up vote
            3
            down vote



            accepted







            up vote
            3
            down vote



            accepted






            I would go with the chunk method:



            cars.chunk { |c| c[:speed]>320 ? :above_320 : :the_rest }.to_h


            Though it's important to underline that chunk doesn't work if the array is not ordered (by speed in this case). In that case we need to prepend it with sort_by:



            cars.sort_by {|car| car[:speed]}.chunk { |c| c[:speed]>320 ? :above_320 : :the_rest }.to_h


            (Moreover, if we want to order the hash keys, like in the example, we need to call sort after chunk):



            cars.sort_by {|car| car[:speed]}.chunk { |c| c[:speed]>320 ? :above_320 : :the_rest }.sort.to_h





            share|improve this answer














            I would go with the chunk method:



            cars.chunk { |c| c[:speed]>320 ? :above_320 : :the_rest }.to_h


            Though it's important to underline that chunk doesn't work if the array is not ordered (by speed in this case). In that case we need to prepend it with sort_by:



            cars.sort_by {|car| car[:speed]}.chunk { |c| c[:speed]>320 ? :above_320 : :the_rest }.to_h


            (Moreover, if we want to order the hash keys, like in the example, we need to call sort after chunk):



            cars.sort_by {|car| car[:speed]}.chunk { |c| c[:speed]>320 ? :above_320 : :the_rest }.sort.to_h






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited 22 hours ago









            sekmo

            1135




            1135










            answered yesterday









            Marc Rohloff

            2,93236




            2,93236












            • Thanks! It's a thing that popped up from a pet project actually :-)
              – sekmo
              yesterday




















            • Thanks! It's a thing that popped up from a pet project actually :-)
              – sekmo
              yesterday


















            Thanks! It's a thing that popped up from a pet project actually :-)
            – sekmo
            yesterday






            Thanks! It's a thing that popped up from a pet project actually :-)
            – sekmo
            yesterday














            up vote
            1
            down vote













            I think that .map.with_index {|cars,i| [ i == 0 ? :above_320 : :the_rest, cars ]} is a bit more verbose and awkward than it needs to be.



            Hash[
            [:above_320, :the_rest].zip(cars.partition { |car| car[:speed] > 320 })
            ]


            Alternatively,



            [:above_320, :the_rest]
            .zip(cars.partition { |car| car[:speed] > 320 })
            .to_h





            share|improve this answer



























              up vote
              1
              down vote













              I think that .map.with_index {|cars,i| [ i == 0 ? :above_320 : :the_rest, cars ]} is a bit more verbose and awkward than it needs to be.



              Hash[
              [:above_320, :the_rest].zip(cars.partition { |car| car[:speed] > 320 })
              ]


              Alternatively,



              [:above_320, :the_rest]
              .zip(cars.partition { |car| car[:speed] > 320 })
              .to_h





              share|improve this answer

























                up vote
                1
                down vote










                up vote
                1
                down vote









                I think that .map.with_index {|cars,i| [ i == 0 ? :above_320 : :the_rest, cars ]} is a bit more verbose and awkward than it needs to be.



                Hash[
                [:above_320, :the_rest].zip(cars.partition { |car| car[:speed] > 320 })
                ]


                Alternatively,



                [:above_320, :the_rest]
                .zip(cars.partition { |car| car[:speed] > 320 })
                .to_h





                share|improve this answer














                I think that .map.with_index {|cars,i| [ i == 0 ? :above_320 : :the_rest, cars ]} is a bit more verbose and awkward than it needs to be.



                Hash[
                [:above_320, :the_rest].zip(cars.partition { |car| car[:speed] > 320 })
                ]


                Alternatively,



                [:above_320, :the_rest]
                .zip(cars.partition { |car| car[:speed] > 320 })
                .to_h






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited yesterday

























                answered yesterday









                200_success

                127k15148410




                127k15148410






















                    sekmo is a new contributor. Be nice, and check out our Code of Conduct.










                     

                    draft saved


                    draft discarded


















                    sekmo is a new contributor. Be nice, and check out our Code of Conduct.













                    sekmo is a new contributor. Be nice, and check out our Code of Conduct.












                    sekmo is a new contributor. Be nice, and check out our Code of Conduct.















                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f207607%2fpartition-array-to-hash-in-ruby%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

                    Morgemoulin

                    Scott Moir

                    Souastre