How do I transpose two hashes in ruby to make a single hash or array?What is the “right” way to iterate through an array in Ruby?Secure hash and salt for PHP passwordsHow can I generate an MD5 hash?How does a hash table work?How to write a switch statement in RubyHow to get a specific output iterating a hash in Ruby?Check if a value exists in an array in RubyHow to convert a ruby hash object to JSON?Array to Hash RubyHow to add new item to hash

Why can my keyboard only digest 6 keypresses at a time?

Advantages of the Exponential Family: why should we study it and use it?

What would be the way to say "just saying" in German? (Not the literal translation)

std::declval vs crtp, cannot deduce method return type from incomplete type

A map of non-pathological topology?

What is the meaning of the Russian idiom "to taste tuna" ("отведать тунца")?

Is it possible to fly backward if you have really strong headwind?

Who won a Game of Bar Dice?

Can a human be transformed into a Mind Flayer?

What are some really overused phrases in French that are common nowadays?

Can I utilise a baking stone to make crepes?

PDF vs. PNG figure: why does figure load so much faster even if file sizes are the same?

Longest bridge/tunnel that can be cycled over/through?

Should I refuse being named as co-author of a bad quality paper?

Is there a DSLR/mirorless camera with minimal options like a classic, simple SLR?

Is it expected that a reader will skip parts of what you write?

Is there a set of positive integers of density 1 which contains no infinite arithmetic progression?

How do free-speech protections in the United States apply in public to corporate misrepresentations?

AMPScript SMS InsertDE() function not working in SMS

Teaching a class likely meant to inflate the GPA of student athletes

60s or 70s novel about Empire of Man making 1st contact with 1st discovered alien race

Bb13b9 confusion

Does putting salt first make it easier for attacker to bruteforce the hash?

Excel division by 0 error when trying to average results of formulas



How do I transpose two hashes in ruby to make a single hash or array?


What is the “right” way to iterate through an array in Ruby?Secure hash and salt for PHP passwordsHow can I generate an MD5 hash?How does a hash table work?How to write a switch statement in RubyHow to get a specific output iterating a hash in Ruby?Check if a value exists in an array in RubyHow to convert a ruby hash object to JSON?Array to Hash RubyHow to add new item to hash






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















I want to take the two hashes below and combine them into a new hash or array:



hash1 = 1=>"]", 2=>"", 3=>")", 4=>"(", 5=>"", 6=>"["
hash2 = 1=>"[", 2=>"", 3=>"(", 4=>")", 5=>"", 6=>"]"


I want the result to look something like this:



result = "["=>"]", ""=>"", "("=>")"


or



result = [ ["[","]"], ["",""], ["(",")"] ]


Is there a ruby method that can do this?










share|improve this question




























    1















    I want to take the two hashes below and combine them into a new hash or array:



    hash1 = 1=>"]", 2=>"", 3=>")", 4=>"(", 5=>"", 6=>"["
    hash2 = 1=>"[", 2=>"", 3=>"(", 4=>")", 5=>"", 6=>"]"


    I want the result to look something like this:



    result = "["=>"]", ""=>"", "("=>")"


    or



    result = [ ["[","]"], ["",""], ["(",")"] ]


    Is there a ruby method that can do this?










    share|improve this question
























      1












      1








      1








      I want to take the two hashes below and combine them into a new hash or array:



      hash1 = 1=>"]", 2=>"", 3=>")", 4=>"(", 5=>"", 6=>"["
      hash2 = 1=>"[", 2=>"", 3=>"(", 4=>")", 5=>"", 6=>"]"


      I want the result to look something like this:



      result = "["=>"]", ""=>"", "("=>")"


      or



      result = [ ["[","]"], ["",""], ["(",")"] ]


      Is there a ruby method that can do this?










      share|improve this question














      I want to take the two hashes below and combine them into a new hash or array:



      hash1 = 1=>"]", 2=>"", 3=>")", 4=>"(", 5=>"", 6=>"["
      hash2 = 1=>"[", 2=>"", 3=>"(", 4=>")", 5=>"", 6=>"]"


      I want the result to look something like this:



      result = "["=>"]", ""=>"", "("=>")"


      or



      result = [ ["[","]"], ["",""], ["(",")"] ]


      Is there a ruby method that can do this?







      ruby hash enumerable






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 24 at 19:59









      Richard JarramRichard Jarram

      5617




      5617






















          4 Answers
          4






          active

          oldest

          votes


















          4














          You could use Hash#transform_keys:



          res = hash1.transform_keys hash2[k] 

          res #=> "["=>"]", ""=>"", "("=>")", ")"=>"(", ""=>"", "]"=>"["

          res.first(3) #=> [["[", "]"], ["", ""], ["(", ")"]]





          share|improve this answer


















          • 1





            Lovely!........

            – Cary Swoveland
            Mar 24 at 21:33


















          1














          Well, another way you can get what you want is by using Hash#deep_merge
          like so:



          res = hash1.deep_merge(hash2) [other_val , this_val] .values
          # => [["[", "]"], ["", ""], ["(", ")"], [")", "("], ["}", " 














          0














          hash1.merge(hash2) [v1, v2].values
          # => [["]", "["], ["", "improve this answer










          answered Mar 24 at 20:07









          iGianiGian

          6,0402828




          6,0402828







          • 1





            Lovely!........

            – Cary Swoveland
            Mar 24 at 21:33















          1














          Well, another way you can get what you want is by using Hash#deep_merge
          like so:



          res = hash1.deep_merge(hash2) [other_val , this_val] .values
          # => [["[", "]"], ["", ""], ["(", ")"], [")", "("], ["", ""], ["]", "["]
          res.first(3)
          # => [["[", "]"], ["", ""], ["(", ")"]]





          share", ""], ["]", "["]
          res.first(3)
          # => [["[", "]"], ["", ""], ["(", ")"]]





          share", "improve this answer















          hash1.each_with_object() 

          #=> "["=>"]", ""=>"", "("=>")", ")"=>"(", ""=>"", "]"=>"["


          Or:



          hash2.each_with_object() h[v] = hash1[k] 

          #=> "["=>"]", ""=>"", "("=>")", ")"=>"(", ""=>"", "]"=>"["






          share", ""], [")", "("], ["(", ")"], ["", ""], ["[", "]"]]





          share", "improve this answer






























            hash1.merge(hash2) [v1, v2].values
            # => [["]", "["], ["", "{"], [")", "("], ["(", ")"], ["", ""], ["[", "]"]]






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Mar 25 at 4:57









            sawasawa

            134k31215311




            134k31215311



























                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%2f55327997%2fhow-do-i-transpose-two-hashes-in-ruby-to-make-a-single-hash-or-array%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

                How to preserve electronics (computers, tablets and phones) for hundreds of years Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) The network's official Twitter account is up and running again. What content…How might modern humans leave a message for 50,000 years?How would an aquatic race develop computers?How do we make computers care for human life?Keeping supplies for 1000 yearsWhat will cell phones become in 100 years?What body armor protects against a laser?Could a society attain 1800s technology if limited to using copper and bronze?Electricity power (magic) and electronicsScavenging metal resources in post-apocalypse after 100 yearsWhy would precursors create devices that can survive and still work after hundreds of years?

                155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해

                Convert JSON to MapSimplest way to convert a string representing JSON array into Java array List of key value pairsHow to take String JSON values into HashMapConvert a JSON String to a HashMapCreating BSON object from JSON stringJSONObject - How to get a value ?Better Map ConstructorReading JSON from Url javaUpdate columns if input values are not null otherwise ignore and keep the existing values of column in databaseHow to store object represented as Json string into Couchbase Lite?Go JSON into MapHow do I efficiently iterate over each entry in a Java Map?Sort a Map<Key, Value> by valuesHow to round a number to n decimal places in JavaCan comments be used in JSON?How do I read / convert an InputStream into a String in Java?How can I pretty-print JSON in a shell script?What is the correct JSON content type?How can I deserialize JSON to a simple Dictionary<string,string> in ASP.NET?Why does Google prepend while(1); to their JSON responses?Parse JSON in JavaScript?