ruby on rails - How to add up the number representations of the objects of the hashes in an array of hashes? -


हैश की एक सरणी

  [{color: "red", आकार: " बड़े "}, {रंग:" नीला ", आकार:" छोटा "}, {रंग: बैंगनी, आकार:" मध्यम "}, {रंग:" गुलाबी ", आकार:" छोटा "}]   

इसके अलावा एक और हैश:

  {छोटा: 1, मध्यम: 2, बड़ा: 3, x_large: 4}   

कैसे होगा सभी आकारों को उनके संबंधित नंबरों से बदल दें और उन संख्याओं को जोड़ दें?

समस्या को ऐसे आकारों की जगहों से बदलकर मैन्युअल रूप से हल किया जा सकता है:

  [{रंग : "लाल", आकार: 3}, {रंग: "नीला", आकार: 1}, {रंग: बैंगनी, आकार: 2}, {रंग: "गुलाबी", आकार: 1}]   

फिर उन आकारों को जोड़कर, आकारों का प्रतिनिधित्व करना होगा आउटपुट: 7

फिर से बदलना अपनी पहली सरणी, दूसरे सरणी से corrospoding आकार के साथ : आकार की जगह:

  arr = [{color: "red", आकार: "बड़ा"}, {रंग: "नीला", आकार: "छोटा"}, {रंग: "बैंगनी", आकार: "मध्यम"}, {रंग: "गुलाबी", आकार: "छोटा"}] आकार = {छोटा: 1, मध्यम: 2, बड़ा: 3, x_large: 4} arr.each do | x | X [: size] = आकार [x [: आकार] .to_sym] end    

Comments

Popular posts from this blog

c# - passing input text from view to contoller with FacebookContext using Facebook app -

ios - Does Core Data autoupdate a many to many relationship on saving -

Calling a C++ function from C# by passing a string with variable size to it -