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

                SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

                은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현