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;
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
add a comment |
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
add a comment |
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
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
ruby hash enumerable
asked Mar 24 at 19:59
Richard JarramRichard Jarram
5617
5617
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
You could use Hash#transform_keys:
res = hash1.transform_keys hash2[k]
res #=> "["=>"]", ""=>"", "("=>")", ")"=>"(", ""=>"", "]"=>"["
res.first(3) #=> [["[", "]"], ["", ""], ["(", ")"]]
1
Lovely!........
– Cary Swoveland
Mar 24 at 21:33
add a comment |
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
# => [["[", "]"], ["", ""], ["(", ")"], [")", "("], ["}", "
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
add a comment ", "
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)
# => [["[", "]"], ["", ""], ["(", ")"]]
hash1.each_with_object()
#=> "["=>"]", ""=>"", "("=>")", ")"=>"(", ""=>"", "]"=>"["
Or:
hash2.each_with_object() h[v] = hash1[k]
#=> "["=>"]", ""=>"", "("=>")", ")"=>"(", ""=>"", "]"=>"["
add a comment ", "improve this answer
hash1.merge(hash2) [v1, v2].values
# => [["]", "["], ["", "{"], [")", "("], ["(", ")"], ["", ""], ["[", "]"]]
answered Mar 25 at 4:57
sawasawa
134k31215311
134k31215311
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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