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

                Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript